Created
January 11, 2026 20:51
-
-
Save Space-Banane/06e755954e4cd2f884b37e1243c24b89 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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 | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works best on a domain network.