$serverName = "localhost"
$sessions = qwinsta /server $serverName | ?{ $_ -notmatch '^ SESSIONNAME' } | %{
$item = "" | Select "Active", "SessionName", "Username", "Id", "State", "Type", "Device"
$item.SessionName = $_.Substring(1,18).Trim()
$item.Username = $_.Substring(19,20).Trim()
$item.Id = $_.Substring(39,9).Trim()
$item.State = $_.Substring(48,8).Trim()
$item.Type = $_.Substring(56,12).Trim()
$item
}
foreach ($session in $sessions) {
if ($session.Username -ne "" -or $session.Username.Length -gt 1) {
if ($session.State -eq "Disc") {
Write-Host ("Logged off {0}" -f $session.Username)
logoff /server $serverName $session.Id
}
}
}