A remoting session is a PowerShell object representing an established connection to another computer and the connection information needed to use it. New-PSSession creates the object and keeps it in PowerShell memory; Get-PSSession retrieves open sessions later. Sessions reduce repeated connection setup and support interactive shells or commands sent to several computers. section 18.1
Use session objects to manage remote work:
- Store returned objects in variables when the same computers will be contacted repeatedly. section 18.1
- Pass one session to Enter-PSSession for an interactive remote shell; Exit-PSSession ends the shell but leaves the session open. section 18.2
- Pass several sessions to Invoke-Command to run a command in parallel across their remote computers. section 18.3
- Remove unused sessions with Remove-PSSession because active sessions consume local and remote resources. section 18.1
Create, inspect, and use sessions
Create reusable connections with New-PSSession, for example `$iis_servers = New-PSSession -ComputerName web01,web02,web03`. Retrieve them with `Get-PSSession`, inspect their ComputerName property with `Get-Member`, and select one for an interactive shell with `Get-PSSession -ComputerName SRV02 | Enter-PSSession`. For batch work, use `Invoke-Command -Command { Get-Process } -Session $iis_servers`. section 18.1 section 18.2 section 18.3
PS C:\> new-pssession -computername srv02,dc17,print99 PS C:\> new-pssession -hostname LinuxWeb01,srv03
Compare session creation over WinRM with -ComputerName and SSH with -Hostname. section 18.1
PS C:\> get-pssession
List the sessions currently maintained by PowerShell. section 18.1
PS C:\> invoke-command -command { Get-Process } -session $session01
Run one command against every session stored in a variable. section 18.3
PS C:\> Disconnect-PSSession -Id 4 Id Name ComputerName State -- ----------------- ------------- ----- 4 Session4 COMPUTER2 Disconnected
Disconnect a session while leaving its PowerShell process running remotely, then reconnect it later with Connect-PSSession. section 18.5
