Get the PowerShell sessions (PSSessions) in the current session.
Syntax Get-PSSession [[-ComputerName] string] [CommonParameters] Get-PSSession [-Id] Int32 [CommonParameters] Get-PSSession [-InstanceId Guid] [CommonParameters] Get-PSSession [-Name string] [CommonParameters] Key -ComputerName string Get only the PSSessions that are connected to the specified computers. Wildcards are permitted. Type the NetBIOS name, an IP address, or a fully-qualified domain name of one or more computers. To specify the local computer, type the computer name, "localhost", or a dot (.). -Id Int32 Get only the PSSessions with the specified IDs. Type one or more IDs (separated by commas), or use the range operator (..) to specify a range of IDs. An ID is an integer that uniquely identifies the PSSession in the current session. It is easier to remember and type than the InstanceId, but is unique only within the current session. To find the ID of a PSSession, use Get-PSSession without parameters. -InstanceId Guid Get only the PSSessions with the specified instance IDs. The InstanceID is a GUID that uniquely identifies a PSSession in the current session. The InstanceID is unique, even when you have multiple sessions running on a single computer. The InstanceID is stored in the InstanceID property of the object that represents a PSSession. To find the InstanceID of the PSSessions in the current session: "Get-PSSession | format-table Name, ComputerName, InstanceId". -Name string Get only the PSSessions with the specified friendly names. Wildcards are permitted. To find the names of the PSSessions in the current session, type "get-pssession" without parameters. CommonParameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable, -OutBuffer -OutVariable.
Standard Aliases for Get-PSSession: gsn
Get-PSSession gets the Windows PowerShell sessions (PSSessions) that were created in the current session.
Without parameters, Get-PSSession gets all of the PSSessions created in the current session. You can use the parameters of Get-PSSession to get the sessions that are connected to particular computers, or you can identify sessions by their names, IDs, or instance IDs.
For more information about Windows PowerShell sessions, see about_PSSessions.
Get all of the PSSessions that were created in the current session. :
PS C:> get-pssession
Get the PSSessions that are connected to the Server64 computer and save them in the $sess variable:
PS C:> $sess = get-pssession -computername Server64
Save the results of a Get-PSSession command in multiple variables:
When PowerShell assigns the first object to the first variable, the second object to the second variable, and so on. If there are more objects than variables, it assigns all remaining objects to the last variable in the array.
PS C:> new-pssession -computername Server64, Server65, Server66
PS C:> $s64, $s65, $s66 = get-pssession
Get a PSSession using its instance ID, and then delete the PSSession.:
PS C:> get-pssession | format-table -property computername, InstanceID
PS C:> $sess = get-pssession -InstanceID fc3e6dfb-f342-253d-7fa4-1abdfc64ae84
PS C:> remove-pssession -session $sess
Get all the PSSessions that connect to computers with computer names that begin with "mail":
PS C:> get-pssession -computername mail*
Get the PSSession with ID = 4:
PS C:> get-pssession 4
“Remove advertising, disable a person or firm from proclaiming its wares and their merits, and the whole of society and of the economy is transformed. The enemies of advertising are the enemies of freedom” - David Ogilvy (Scottish/British military intelligence officer and later top advertising executive)
Related PowerShell Cmdlets:
Enter-PSSession - Start an interactive session with a remote computer.
New-PSSession - Create a persistent connection to a local or remote computer.
Remove-PSSession - Close PowerShell sessions.