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.
Table of Contents
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
andTargetUser
cannot be used together. If the Start-PSCRemoteLogoff executed withoutLogoffAll
orTargetUser
, theLogoffAll
will be activated by default.
Get-PSCActiveSession Examples
Starting with Get-PSCActiveSession
to get session information from MyServer
Get-PSCActiveSession -Name "MyServer1"
And in the case of multi-user logins, it will show as the following
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
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
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
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
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
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
Thanks for this script. Question: How can I execute it once and automatically logoff ONLY users with “Disconnected” SessionSate?
Hi, Thanks for the comment.
Currently, it’s not possible, but I will update the script to include such an option.
So it will be like
Start-PSCRemoteLogoff -DisconnectedOnly
Stay tuned for the update, and if you have any other recommendations, feel free to add it all so I can do an update for multiple features at once.
HI
I had updated the module to include -DisconnectedOnly parameter.
Get the lastest version from here
https://www.powershellgallery.com/packages/Get-ActiveSession/1.0.3
I have tested and it works well for me so far. Thank you for the speedy update and help!
Hi. We get a lot of user requests to reset their sessions and I’m thinking about creating some sort of self service so the user can reset his own disconnected remote session. I’m planning to do this by developing a C Sharp Windows Forms app and my question is: Do I have to install the PS module on every PC for every user?
Hi,
Yes, the module should be installed on all PC.
It’s possible for me to update the script and include a new parameter called
Server
. This new parameter allows the script to work from a server-side and read from a shared folder.Use your C Sharp client-side app to create a .txt file that includes the username of the user.
On the server, the script reads the created txt file and disconnects users.
This also can be done using HTTP instead of a .txt file.
What do you think?
Hi,
I tried the code but I always get the feedback:
It Seems there was an issue for xxxxxxxxxxx
Hi, The script uses the “query.exe” Windows native tool in this part. and if the result is empty this indicates that the host is not reachable or there is no remote session in the server.
you can try to run the tool from cmd and let me know the result
query user /server:%YOUR_SERVER_NAME%
The output should be a message stating there is no active session or another possibility is the tool is unable to connect.
Feel free and share more details with me to farisnt@gmail.com
Thanks
Hello, Faris
If if your .nupkg uses query user
Is it possible for “Get-PSCActiveSession” output sessions with status “inactive\disconnected”, and information like idle time, login time
Is it possible to sort the output in alphabetical order by name instead of Session Number?
will check this and try to update it, I got multiple other request for this module