How to sign out all disconnected RDP sessions using PowerShell

Introduction

Remote Desktop Protocol (RDP) is a widely-used technology that allows users to connect to remote computers over the network. However, if RDP sessions are not disconnected properly, they can accumulate on the server and take up valuable resources. In this article, we will show you how to use PowerShell 5.1 to sign out all disconnected RDP sessions on a local or remote server.

Step 1: Check the PowerShell version

Before we begin, let’s make sure that we have the correct version of PowerShell installed. To do this, open PowerShell and run the following command:

$PSVersionTable.PSVersion

This should display the PowerShell version information. If the version number is not 5.1 or higher, you may need to upgrade PowerShell before continuing.

Step 2: Query RDP sessions

To sign out disconnected RDP sessions, we first need to identify which sessions are disconnected. We can do this by using the query user command, which displays information about all active RDP sessions on a local or remote server.

Here’s the PowerShell script to query RDP sessions:

$queryOutput = query user /server:$env:COMPUTERNAME
$discSessions = $queryOutput | Where-Object { $_ -match 'Disc' }
$discSessions

This script retrieves information about all active RDP sessions on the local machine and stores the output in the $queryOutput variable. The output is then filtered for disconnected sessions using the Where-Object cmdlet, which matches any line that contains the word “Disc”. The filtered output is stored in the $discSessions variable.

Step 3: Sign out disconnected sessions

Now that we have a list of disconnected sessions, we can sign them out using the logoff command. Here’s the updated PowerShell script:

$queryOutput = query user /server:$env:COMPUTERNAME
$discSessions = $queryOutput | Where-Object { $_ -match 'Disc' } | ForEach-Object {
    $cols = ($_ -split '\s+')
    $sessionId = $cols[2]
    logoff $sessionId /server:$env:COMPUTERNAME
}

This script uses the same query user command as before to retrieve information about all active RDP sessions on the local machine. The output is then filtered for disconnected sessions using the Where-Object cmdlet, just like before.

However, instead of simply displaying the list of disconnected sessions, the $discSessions variable is piped to the ForEach-Object cmdlet to process each disconnected session. For each session, the session ID is extracted from the output and stored in the $sessionId variable. The logoff command is then used to sign out the disconnected session with the specified session ID and the /server parameter is set to $env:COMPUTERNAME to specify the local machine as the target server.

In this article, we showed you how to use PowerShell 5.1 to sign out all disconnected RDP sessions on a local or remote server. By regularly signing out disconnected sessions, you can free up valuable resources on the server and ensure that the RDP environment remains efficient and secure.

Note: Depending on your environment, you may need to run the script with elevated privileges to access the Remote Desktop session information and sign out the disconnected users.

********************************************************

If you liked what you read Please Share.
I’d love it if you followed me on YouTube and Facebook.

Also, feel free to subscribe to my posts by email.
Donations for the site can be made here.
Thanks for reading.

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *