user1994521
user1994521

Reputation: 87

Show all users with "disconnected" state

I need powershell script which show me all users on server in disconnected state. I tried googling but with no luck.

Upvotes: 2

Views: 1624

Answers (1)

4c74356b41
4c74356b41

Reputation: 72151

quser /server:serverName is the command you are looking for, you could further parse it with PowerShell pretty easily. I don't think there's a more native way.

$username = @()
$sessions = @()
$output = (quser /server:$serverName | select -Skip 1).substring(1)
foreach ($line in $output) {
    if ($line -match 'disc') {
    $username += ($line -split '\s+')[0]
    $sessions += ($line -split '\s+')[2]
    }
}

Also, if you want to logout disconnected users:

foreach ($session in $sessions) {
            logoff $session /server:$Servername
        }

Upvotes: 4

Related Questions