Disconnect-LoggedOnUser – Disconnect a RDP session remotely

I created a new function that wraps around the rwinsta commandline-tool to remotely disconnect RDP sessions. I created this function to simplify cleaning up open RDP sessions, for example when an administrator leaves the company there might be disconnected RDP sessions by this user. This scripts works in combination with the Get-LoggedOnUser script to simplify finding and disconnecting these sessions. The script is available for download in the TechNet Script Gallery: Disconnect-LoggedOnUser.

A simple example of how to use this function is as follows:

Disconnect-LoggedOnUser -ComputerName server01 -Id 5 
Disconnect Session Id 5 on server01 

In the next example the output of the Get-LoggedOnUser script is filtered by Where-Object and then piped into the Disconnect-LoggedOnUser script:

.\Get-LoggedOnUser.ps1 -ComputerName server01,server02 | Where-Object {$_.UserName -eq 'JaapBrasser'} | Disconnect-LoggedOnUser -Verbose 
Use the Get-LoggedOnUser script to gather the user sessions on server01 and server02. Where-Object filters out only the JaapBrasser user account and then disconnects the session by piping the results into Disconnect-LoggedOnUser while displaying verbose information. 

The complete function is available in the TechNet Script Library. To view this script or to participate in the discussions about this script either comment here or in the TechNet Script Gallery.

TechNet Script Gallery
My entries in TechNet Script Gallery

Leave a Reply