PSSessions and Remoting
PowerShell Remoting lets you run commands on remote computers as if you were sitting at their console. It uses the WS-Management (WinRM) protocol on Windows and SSH on cross-platform setups.
Enabling Remoting
Before you can connect to a remote machine, remoting must be enabled on it:
Enable-PSRemoting -ForceThis configures the WinRM service, creates a listener, and sets firewall rules. It requires administrator privileges on the target machine.
Key Concept: On Windows Server, PSRemoting is enabled by default. On client editions of Windows, you must run Enable-PSRemoting manually.
Interactive Sessions with Enter-PSSession
Enter-PSSession opens a one-to-one interactive session on a remote computer. Your prompt changes to show the remote machine name:
Enter-PSSession -ComputerName Server01
[Server01]: PS C:\> Get-Service | Where-Object Status -eq 'Running'
[Server01]: PS C:\> Exit-PSSessionOne-Off Commands with Invoke-Command
Invoke-Command runs a script block on one or more remote computers without entering an interactive session:
Invoke-Command -ComputerName Server01, Server02 -ScriptBlock {
Get-Process | Sort-Object CPU -Descending | Select-Object -First 5
}Results automatically include a PSComputerName property so you can tell which machine each result came from.
Persistent Sessions with New-PSSession
When you need to run multiple commands that share state (variables, imported modules), create a persistent session with New-PSSession:
$session = New-PSSession -ComputerName Server01
Invoke-Command -Session $session -ScriptBlock { $data = Get-Service }
Invoke-Command -Session $session -ScriptBlock { $data.Count }
Remove-PSSession $sessionKey Concept: Temporary connections (via -ComputerName) create and destroy a session for each command. Persistent sessions (via New-PSSession) keep state between calls. Always clean up with Remove-PSSession.
Using Credentials
When connecting to machines where your current account lacks permissions, provide credentials:
$cred = Get-Credential
Enter-PSSession -ComputerName Server01 -Credential $cred
Invoke-Command -ComputerName Server01 -Credential $cred -ScriptBlock { hostname }Running Local Scripts Remotely
You can run an entire local script on remote machines without copying the file:
Invoke-Command -ComputerName Server01, Server02 -FilePath C:\Scripts\Audit.ps1PowerShell reads the local file and sends its contents to the remote machines for execution.
Exercises
Write the command to start an interactive remoting session on a computer named 'FileServer01'.