Powershell

How to Use the Get-PSSession (Microsoft.PowerShell.Core) Cmdlet in PowerShell?

The “Get-PSSession” cmdlet is used to get the PowerShell sessions managed by users on local and remote systems. When the “Get-PSSession” cmdlet gets executed without any parameters it gets the sessions that are created in the current session. Using the “ComputerName” parameters along with the “Get-PSSession” cmdlet will get the sessions that are connected to the local computers or remote computers, even if they are not created in the current session. “gsn” is the standard alias for the “Get-PSSession” cmdlet.

This article will elaborate on the use of PowerShell’s “Get-PSSession” cmdlet.

How to Use the Get-PSSession Cmdlet in PowerShell?

The “Get-PSSession” cmdlet is used to retrieve the local or remote system sessions that are created in the current PowerShell session. To get the PowerShell session, simply execute the “Get-PSSession” cmdlet in the console.

Syntax
The syntax for the “Get-PSSession” cmdlet is provided below:

Get-PSSession [-Name] [CommonParameters]

Example 1: Use the “Get-PSSession” Cmdlet to Get Sessions Created in the current session
Use the following command to get the sessions created in the current session:

Get-PSSession

Example 2: Use the “Get-PSSession” Cmdlet to Get Sessions Connected to the Local Computer
Specify the “Get-PSSession” cmdlet along with the “-ComputerName” parameter and assign the computer name to get sessions connected to the local system:

Get-PSSession -ComputerName "localhost"

Example 3: Use the “Get-PSSession” Cmdlet to Get a Session by Using its ID
To get the session by Id, first, use the “Get-PSSession” cmdlet along with the “-Id” parameter and assign the Id:

Get-PSSession -Id 2

That’s it! You have learned the usage of the “Get-PSSession” cmdlet in PowerShell.

Conclusion

The “Get-PSSession” cmdlet is used to retrieve the PowerShell sessions on local and remote computers. To use the PSSession, the user’s local and remote computers must be set up for remote access. Additionally, it can also retrieve the PowerShell sessions generated in the current session. This post has explained the usage of PowerShell’s “Get-PSSession” cmdlet.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.