Get PSSession - PowerShell


Overview

Get-PSSession obtains details about active PowerShell sessions, both local and remote. It provides insights into session properties, such as state, computer name, username, and other relevant information. This command is useful for managing, monitoring, and troubleshooting PowerShell sessions across multiple systems.

Syntax

Get-PSSession [-Id] <session ID(s)> [-ComputerName] <computer name(s)> [-State] <session state(s)>
              [-Verbose] [-Confirm] [-WhatIf] [-ErrorAction] <Action> [-ErrorVariable] <Variable>

Options/Flags

  • -Id (String[]): Specifies the ID(s) of the session(s) to retrieve information about.
  • -ComputerName (String[]): Filters sessions based on the computer(s) they are running on.
  • -State (String[]): Filters sessions by their current state, such as ‘Idle’, ‘Running’, etc.
  • -Verbose (Switch): Outputs detailed information about the session(s).
  • -Confirm (Switch): Prompts for confirmation before executing the command.
  • -WhatIf (Switch): Displays what the command would do without actually executing it.
  • -ErrorAction (ActionPreference): Specifies the action to take when an error occurs.
  • -ErrorVariable (Variable): Assigns any errors encountered to the specified variable.

Examples

Get all active sessions on the local computer:

Get-PSSession

Get session details for a specific ID:

Get-PSSession -Id "31d34e85-de16-4dc5-b6b1-9c30547c5471"

Filter sessions by state:

Get-PSSession -State "Running"

Get verbose output for a session:

Get-PSSession -Id "de8e197c-b0e5-451e-a57a-6087e1e22052" -Verbose

Common Issues

  • No sessions found: Ensure that the specified filters are correct and that active sessions exist on the target systems.
  • Permission denied: The user may not have sufficient permissions to retrieve session information on certain systems.
  • Invalid session ID: Make sure the provided session ID is valid and corresponds to an existing session.

Integration

Get-PSSession can be used in combination with other PowerShell commands to perform advanced tasks:

  • Export-PSSession to save session information to a file.
  • New-PSSession to create new PowerShell sessions.
  • Invoke-Command to execute commands on remote sessions.
  • Stop-PSSession to terminate active sessions.