Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remotely log off all disconnected sessions from a server, RDP session hosts or otherwise.
$server = ''
$sessions = quser /server:$server | where-object { $_ -match "Disc" }
Foreach ($Session in $Sessions)
{
$UserName = $Session.split(' +')[1]
$ID = qwinsta /server:$server $UserName | where-object { $_ -match $UserName } | Where { $_ -ne "" } | ForEach { $_.Replace(" ","") } | ForEach { $_.Replace("Disc","") } | ForEach { $_.ToLower() } | ForEach { $_.Replace("$UserName","") }
Write-Output "SessionID $ID Username: $UserName is disconnected"
logoff $ID /server:$server
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.