Remote Logoff Windows User

I ran into an issue were my RDP session go corrupted on a test server. I couldn’t logoff. I found this page that tells you how to force a logoff through PowerShell.

# Username to logoff
$userName = 'administrator'
# Get the session id from the server.  In this case the Server ID is 'DC'
$sessionId = ((quser /server:DC | Where-Object { $_ -match $userName }) -split ' +')[2]
# Display the session
$sessionId
# Logoff the Session ID from server DC
logoff $sessionId /server:DC

If you think hard about this you could put this into a script. Just don’t call it ‘logoff.ps1’ since that is a reserved keyword in PowerShell.