Skip to content

Instantly share code, notes, and snippets.

@AshFlaw
Created September 30, 2018 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AshFlaw/df89a7756e575c6c4a2f4a55ef3b7f6a to your computer and use it in GitHub Desktop.
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.
$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