Skip to content

Instantly share code, notes, and snippets.

@Space-Banane
Created January 11, 2026 20:51
Show Gist options
  • Select an option

  • Save Space-Banane/06e755954e4cd2f884b37e1243c24b89 to your computer and use it in GitHub Desktop.

Select an option

Save Space-Banane/06e755954e4cd2f884b37e1243c24b89 to your computer and use it in GitHub Desktop.
Invoke-Command -ComputerName "TARGETCOMPUTER" -ScriptBlock {
$sessions = query user
# Use a more robust regex to grab the ID regardless of the SessionName column being blank
$sessions | Select-String -Pattern '(?<ID>\d+)\s+(?=(Active|Disc))' | ForEach-Object {
logoff $_.Matches.Groups['ID'].Value
}
foreach ($session in $sessions) {
$sessionID = ($session -split ' +')[2]
logoff $sessionID
}
}
@Space-Banane
Copy link
Author

Works best on a domain network.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment