For some reasons it is sometimes necessary to logoff terminal server users who are disconnected except one or selected users.

That is for example on SOLIDWORKS PDM task hosts where you are not able to set which user should run tasks if they are signed on but only one has the appropriated settings to do so.

The script must be run as Administrator.

$Query = query user /server:$server
$Sessions = ConvertFrom-SourceTable $Query

$Sessions | Where-Object {$_.State -eq 'Disc'} | ForEach-Object {
    If ($_.UserName -eq "Srvch_EPDM") {Continue}
    logoff $_.Id /server:$server /v
}

For convenience this script uses a command-let from here: https://github.com/iRon7/ConvertFrom-SourceTable.

Verwandte Artikel