If you are an IT administrator And you’re trying to solve some performance problem on a server one of the most common things to do is to force log off all of the disconnected users.

Using this simple PowerShell script, you can instantly log off all disconnected users, as shown in the screenshots below:

simple script to log off all disconnected users

Here is the PowerShell commands which you can run directly in a PowerShell window (as I did above), or you can run as a .PS1 file.

$DisconnectedSessions = quser | Where-Object { $_ -match 'Disc' } | ForEach-Object { $fields = $_ -split ' +'; @{ UserName = $fields[1]; SessionId = $fields[2] } }
foreach ($Session in $DisconnectedSessions) {
    Write-Host "Logging off user: $($Session.UserName) with session id: $($Session.SessionId)"
    logoff $Session.SessionId

As you can see in the screenshot, this does a great job of logging off disconnected users in one easy step.


Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *