Manage Learn to apply best practices and optimize your operations.

Logoff Disconnected Win10 Users Via Command Line

Saw an interesting question raised on this morning. The thread was entitled “Log off all but current user.” On a PC where multiple users share the same machine, but don’t always log out, the poster sought a way to log off all but the currently active user. Turns out the command line – which means either cmd.exe/Command Prompt or PowerShell – supports this ability. The key is to run either with administrative permissions. Then it’s simple to logoff disconnected Win10 users via command line instructions. Here’s how:

A simple series of commands logs off all disconnected accounts.

Instructions to Logoff Disconnected Win10 Users Via Command Line

The preceding example shows PowerShell, but the same commands work at the Command Prompt (cmd.exe) equally well. Here’s a breakdown:

quser: Queries for users currently logged in. The State of the current login is “Active” and the state for all other logins is “Disc” (for Disconnected). Note also the ID column, which provides a single-digit shorthand for each associated account. (It’s much faster to type “3” than “administrator”!) The final quser command in the preceding window shows that only the active “etitt” account remains logged in.

logoff <ID>: logs the user account associate with the ID from the PC. We can string multiple such commands together using the pipe (vertical bar “|”) character on a single command line. Thus logoff 3 | logoff 4 | logoff 5 is the same as logging off administrator, localonly and xtraadmin accounts.

More Automation, Please!

A single, more eloquent PowerShell command will read the state for each user who’s logged in and log off any whose state equals “Disc” (for disconnected). It’s a beaut (please enter on a single command line; WordPress makes me break it into multiple lines for readability):

quser | Select-String "Disc" |
{logoff ($_.tostring() -split ' +')[2]}

This command can be understood in terms of the following pseudo-code:

  1. Check all logged in users
  2. For each logged in user
  3. Log off user whose state = “Disc”

Pretty neat, especially for lab, library, kiosk, or other systems where one may routinely find numerous disconnected logins hanging ‘round. Good stuff!

Virtual Desktop