How to force logoff a user in Powershell?

Powershell cmdlets can be used to force logoff a user. You could force logoff a user on a remote computer by specifying the computer name on the command line.

Or, you could find the user details from the server he is logged on to and force logoff the user by using a single command.

We review the three ways by which you can force logoff a user using Powershell. For all these methods, you need administrative privileges before running the Powershell cmdlets.

Force logoff a user using quser and logoff

If the user is logged on to the server, you can use the quser command and session id to force logoff the user. The following commands will help you force logoff a user who is logged on through a server.

quser
logoff [session id]

The output of the quser command will give you the session id of the user that needs to be forced to log off from the system. This session id will be used in the logoff command to force logoff of the user.

For example, if the quser reports that the session id for the user to be forced to log off is 10, then the logoff command will look like the one below:

logoff 10

This will force the user with session id 10 to log off.

Force logoff a user using logoff command in Powershell

The logoff command in Powershell can be used to force logoff a user. Before you can run the command, you need to ensure that:

  • You have administrative privileges to run Powershell commands
  • The remote computer needs to run the WinRM service to enable remote administration through Powershell

The logoff command assumes that you are aware of the username of the user who needs to be forced to logoff.

Logoff \\computername -u username

So, if you wish to logoff user dan from the computer name SYSTEM-01, you will use the following command:

Logoff \\SYSTEM-01 -u dan

It is important to note that the logoff command works through the executable file under Windows root. However, logoff.exe and quser.exe are not available on Windows 10 Home edition.

Both commands work well on Windows servers.

Force logoff all users using Stop-computer

You can also choose to force logoff all the users who are logged in to a specific computer. The Stop-computer cmdlet in Powershell will send a stop instruction to the computer and force it to shutdown and logoff all the users.

Stop-computer can be used to stop one or more remote computers. If required, credentials can be passed through the command line in Powershell.

The following command will force logoff all users that are logged on to a computer named SYSTEM-01:

Stop-Computer -ComputerName “SYSTEM-02” -Force

To run the Stop-computer cmdlet, you need administrative privileges.

Force logoff all users using shutdown command

If you do not have access to the logoff command, you can use the shutdown command to force logoff all users connected to a computer. The shutdown executable is also available on Windows 10 Home and other Windows 10 and Windows 11 versions.

The command below will force logoff all the users that are logged on to SYSTEM-01.

shutdown /s /f

To remotely shut down a system, we can use this command:

shutdown /s /m \\computername

So, if you wish to remotely shutdown a computer names SYSTEM-01, the command becomes like the one shared below:

shutdown /s/f \\SYSTEM-01

By default, the shutdown command will work on the local system and shut it down. Using the /m switch and providing the computer name shifts the action to a remote computer.

Summary

In this Powershell tutorial, we have seen that we can forcefully logoff a user or all the users on the local or remote computer using one of the following approaches:

  • Use the logoff command to force logoff a single user or all users logged on to a computer
  • Use the Stop-computer cmdlet to force logoff all users connected to a system
  • Use the shutdown command to force logoff one or all users connected to the system

Suggested Powershell Tutorials

The following Powershell tutorials help in performing basic Windows system administration tasks.

Rajesh Dhawan

Rajesh Dhawan is a technology professional who loves to write about Cyber-security events and stories, Cloud computing and Microsoft technologies. He loves to break complex problems into manageable chunks of meaningful information.