Created
September 30, 2018 19:19
-
-
Save AshFlaw/df89a7756e575c6c4a2f4a55ef3b7f6a to your computer and use it in GitHub Desktop.
Remotely log off all disconnected sessions from a server, RDP session hosts or otherwise.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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