function Remove-DisconnectedSessions {
# Get all user sessions on the machine
$sessions = query user
foreach ($session in $sessions) {
# Split the session details into an array
$sessionDetails = $session -split '\s+'
# Check if the session state is 'Disc' (Disconnected)
if ($sessionDetails[3] -eq 'Disc') {
$sessionId = $sessionDetails[2]
# Log off the disconnected session
logoff $sessionId
Write-Host "Disconnected session with ID $sessionId has been logged off."
}
}
}
Remove-DisconnectedSessions
This can be used in SCCM Script and run against any Device collection
No comments:
Post a Comment