Get Remote Desktop Sessions with PowerShell

Image Description

Daily PowerShell #64

Daily PowerShell Remote Desktop Windows Terminal Services

December 29, 2021

quote Discuss this Article

In this post, we’ll look at how get remote desktop sessions with PowerShell. This post was requested by one of our readers.

Get-ActiveSession Module

The Get-ActiveSession module uses the query session tool to locate sessions and return them as objects. It parses the string output from the tool and provides the session information. The information is minimal but the module is basic.

Install-Module Get-ActiveSession
Get-PSCActiveSession
<#
Name Username SessionState SessionID
---- -------- ------------ ---------
     adamr    Console      3
#>

ConnectedUsers Module

The ConnectedUsers module is specifically designed for retrieving remote user sessions and stopping sessions, if desired. This module provides more functionality than the Get-ActiveSession module. This module executes quser.exe internally.

Install-Module -Name ConnectedUsers
Get-QUser
<#
Id UserName SessionName IsCurrentSession State  IdleTime LogonTime             Host
-- -------- ----------- ---------------- -----  -------- ---------             ----
 3 adamr    console     True             Active          12/29/2021 8:19:00 AM LAPTOP-496LAUK8
#>

Cassia

Cassia is a .NET library for managing Remote Desktop on Windows. You can download and load the DLL to call within PowerShell. Cassia provides the most information but does not have cmdlets available.

Invoke-Webrequest https://www.nuget.org/api/v2/package/Cassia/3.0.0-alpha.9 -Outfile .\cassia.zip
Expand-Archive .\cassia.zip .\cassia
Import-Module .\cassia\lib\netstandard2.0\Cassia.dll
$Manager = [Cassia.TerminalServicesManager]::new()
$LocalServer = $manager.GetLocalServer()
$LocalServer.GetSessions()
$LocalServer.Dispose()
<#
IncomingStatistics : Cassia.Impl.ProtocolStatistics
OutgoingStatistics : Cassia.Impl.ProtocolStatistics
ApplicationName    :
Local              : True
RemoteEndPoint     :
InitialProgram     :
WorkingDirectory   :
ClientProtocolType : Console
ClientProductId    : 0
ClientHardwareId   : 0
ClientDirectory    :
ClientDisplay      : Cassia.Impl.ClientDisplay
ClientBuildNumber  : 0
Server             : Cassia.Impl.TerminalServer
ClientIPAddress    :
SessionIPAddress   :
WindowStationName  : Services
DomainName         :
UserAccount        :
ClientName         :
ConnectionState    : Disconnected
ConnectTime        :
CurrentTime        : 12/29/2021 11:04:26 PM
DisconnectTime     :
LastInputTime      :
LoginTime          :
IdleTime           : 00:00:00
SessionId          : 0
UserName           :

IncomingStatistics : Cassia.Impl.ProtocolStatistics
OutgoingStatistics : Cassia.Impl.ProtocolStatistics
ApplicationName    :
Local              : True
RemoteEndPoint     :
InitialProgram     :
WorkingDirectory   :
ClientProtocolType : Console
ClientProductId    : 0
ClientHardwareId   : 0
ClientDirectory    :
ClientDisplay      : Cassia.Impl.ClientDisplay
ClientBuildNumber  : 0
Server             : Cassia.Impl.TerminalServer
ClientIPAddress    :
SessionIPAddress   :
WindowStationName  : Console
DomainName         : LAPTOP-496LAUK8
UserAccount        : LAPTOP-496LAUK8\adamr
ClientName         :
ConnectionState    : Active
ConnectTime        : 12/29/2021 8:19:37 AM
CurrentTime        : 12/29/2021 11:04:26 PM
DisconnectTime     :
LastInputTime      :
LoginTime          : 12/29/2021 8:19:56 AM
IdleTime           : 00:00:00
SessionId          : 3
UserName           : adamr

IncomingStatistics : Cassia.Impl.ProtocolStatistics
OutgoingStatistics : Cassia.Impl.ProtocolStatistics
ApplicationName    :
Local              : True
RemoteEndPoint     :
InitialProgram     :
WorkingDirectory   :
ClientProtocolType : Console
ClientProductId    : 0
ClientHardwareId   : 0
ClientDirectory    :
ClientDisplay      : Cassia.Impl.ClientDisplay
ClientBuildNumber  : 0
Server             : Cassia.Impl.TerminalServer
ClientIPAddress    :
SessionIPAddress   :
WindowStationName  : 7A78855482A04FA781DC
DomainName         :
UserAccount        :
ClientName         :
ConnectionState    : Listening
ConnectTime        :
CurrentTime        :
DisconnectTime     :
LastInputTime      :
LoginTime          :
IdleTime           : 00:00:00
SessionId          : 65536
UserName           :
#>