RDP considered one of the most widely used protocols to log in to the Windows Remote System. There are multiple built-in tools to get and query RDP session information. But still not that powerful when it comes to bulk queries. So I wrote a PowerShell Module to help in getting RDP session information, and log the user/s off if needed.

Download RDP PowerShell (Get-ActiveSession)

You can download the PowerShell Module from the Microsoft PowerShell Gallery and also you can contribute to my Git repo from this link.

Prerequisits and Support.

This PowerShell module requires Windows PowerShell 5.1. It utilizes the Query.exe and Logoff native tool to get session information parse the result and return it as an object. This script was tested on Windows 10 Client, Windows Server 2012, Windows Server 2016, and Windows Server 2019.

Updates

This module latest update is:

Version 1.0.4 – 13 – Aug -2023:

Error message when no active session is found can be suppressed by using [Switch]IgnoreError

Available PowerShell Commands and Parameters.

This PowerShell module includes two cmdlets:

  • Get-PSCActiveSession: Retrieve current sessions (Console, Active #RDP, or Disconnected). It accepts the following parameters
    • [String]Name: The Computer name to get session information from.
    • [Switch]IgnoreError: This suppresses the error It Seems there was an issue for ServerName Or there is no active session The Error is
  • Start-PSCRemoteLogoff: Logoff All users or a single specific user from a remote RDP Session. This command accepts the following parameters:
    • [String]Name: The Computer name to perform the logoff operation on.
    • [String]TargetUser: The username to logoff, so if there were multiple users logged in to the server, it’s possible to logoff a named user and maintain all other sessions.
    • [Switch]LogoffAll: Logoff all users from the server specified on the Name parameter.
    • [Switch]DisconnectedOnly: Only logoff the disconnected session and keep the active session.

The LogoffAll and TargetUser cannot be used together. If the Start-PSCRemoteLogoff executed without LogoffAll or TargetUser, the LogoffAll will be activated by default.

Get-PSCActiveSession Examples

Starting with Get-PSCActiveSession to get session information from MyServer

Get-PSCActiveSession -Name "MyServer1" 
Get Active Session (Console) from MyServer1

And in the case of multi-user logins, it will show as the following

Multi RDP sessions

This module accepts data from the pipeline and from cmdlets such as Get-ADComputer, so you can use the Get-ADComputer with the filter you need and pipeline it to Get-PSCActiveSession

Get-PSCActiveSession Pipeline

Start-PSCRemoteLogoff Examples

The Start-PSCRemoteLogoff makes it easy for the administrator logs off sessions from multiple servers. For example

Start-PSCRemoteLogoff -Name DC02 -LogoffAll
Logoff all users from DC02 Server

It’s also possible to log off only a certain user from a server, for example, logging off User1 from FS01, while keeping other sessions.

Start-PSCRemoteLogoff -Name FS01 -TargetUser User1
Logging off a single user

This cmdlet also accept pipeline, so it’s possible to get a list of servers and logoff all logged-in users

Get-ADComputer fs01 | Start-PSCRemoteLogoff -LogoffAll
Input from Get-ADComputer

The filter from Get-AdComputer can be a single computer or multiple computers.

The same deal applies to TargetUser parameter, so it’s possible to fetch a list of computers using Get-ADComputer and log off only one user only. For example, a server named FS01 that have User1 and Administrator logged in. Running the following line will only Logout User1

Get-ADComputer FS01 | Start-PSCRemoteLogoff -TargetUser User1
Logging off users base on the input from Get-ADComputer

You can use any filter in the Get-ADComputer, for example, to log off User1 from all the organization computers, use the following command

Get-ADComputer -filter * | Start-PSCRemoteLogoff -TargetUser User1

Logoff only Disconnected Sessions

This parameter logoff only the disconnected session and won’t impact any active session.

Start-PSCRemoteLogoff -Name MyServer -DisconnectedOnly 

Conclusion

This PowerShell module should make RDP session handling easier, try it and let me know if there are any issues or features you like to include in the future. I hope this help, if it does, leave a comment in the section below 🙂

Read More:

A very simple script to generate Complex Password using PowerShell

Learn step-by-step how to write Telegram Bot using PowerShell

5/5 - (1 vote)