There are multiple ways to log off the current user on a Windows system using the command line. The version of the commands below will depend on the Windows version, the interface you are using to log off the user (e.g. Remote Command Prompt, RMM, Local Command Prompt) etc..
The command to force the current user to log off immediately using the command line in Windows is:
shutdown /l /f /t 00
Sometimes you will need to use a “-” instead of a “/”
shutdown -l -f -t 00
If you’d like to log off the session giving the current user the chance to save any work you would just exclude the “/f”
shutdown /l
or
shutdown -l
On some remote management systems you can’t issue the shutdown command. In these cases what you need to do is find out the current user’s active session number by issuing the following command:
query user
You will then be able to see the current user’s Id and issue the logoff command instead:
logoff 1
Where 1 is the ID of the user you are logging off after performing the query user command.
The following command has been tested on Windows XP, Windows 7, Windows 8, Windows 8.1, Windows 10. Your mileage may vary on other version of windows.
on November 25, 2010
We can logoff a user session by clicking on the Logoff button in the start menu. We can logoff from command prompt(CMD) too using shutdown command. You need to run the below command.
shutdown -L
Shutdown
is a Windows in-built command. We don’t have to install it separately. Note that we can run this command from windows Run window also. This command works on all windows editions. (XP, Vista, Server 2k3/2k8, Windows 7)
There’s another command Logoff
which also serves the same purpose. This command can be used to logoff sessions on the remote computers also. Find syntax below for this.
logoff /server:remote_computer_name
To logoff on the current system
logoff
This does not accept user name and passwords so it uses the credentials of the current logged in user on the host system.
How to force logoff without waiting for user confirmation to terminate the running applications?
The above commands do forced logoffs. They kill all the applications one by one and at the end logs off the user.
Managing user sessions in a Windows environment is a critical task for system administrators. There are times when you need to terminate a user session, whether for maintenance, troubleshooting, or security reasons. The Windows Command Line provides powerful tools for managing user sessions efficiently. This article will explore how to kill a user session Windows Command Line, ensuring you can maintain control over your network and systems effectively.
Understanding User Sessions in Windows
A user session in Windows is an instance where a user logs into the system and interacts with it. These sessions are tracked and managed by the system, and administrators often need to monitor and control them. Scenarios where you might need to terminate a user session include:
- Maintenance and Updates: Performing system maintenance or updates that require users to log off.
- Troubleshooting: Addressing issues where a user’s session may be causing system problems.
- Security: Terminating sessions that pose a security threat.
Tools for Managing User Sessions
Windows provides several command-line tools to manage user sessions:
- Task Manager: Provides a graphical interface but can be accessed through the command line.
- Command Line Tools: query, logoff, and tsadmin are powerful commands for session management.
Using Command Line Tools to Kill User Sessions
1. Query User Command
The query user command allows you to list all user sessions on a local or remote system. This is the first step in identifying the session you want to terminate.
Syntax:
CSS Copy code
query user [username] [/server:servername]
Example:
graphql Copy code
query user
This command lists all active user sessions on the local machine, displaying details like session name, user name, ID, and session state.
2. Logoff Command
The logoff command is used to terminate a user session. After identifying the session ID from the query user command, you can use logoff to end the session.
Syntax:
CSS Copy code
logoff [sessionID] [/server:servername]
Example:
Copy code
logoff 2
This command logs off the session with ID 2 on the local machine. If you need to log off a user on a remote server, you would specify the server name.
3. Taskkill Command
The taskkill command is another powerful tool that can be used to terminate user processes and sessions.
Syntax:
CSS Copy code
taskkill /ID [pid] /F
Example:
r Copy code
taskkill /ID 1234 /F
This command forcibly terminates the process with process ID (PID) 1234. To use taskkill for a user session, you need to identify the PID of the user’s processes.
Also read: What Command Launches the Remote Desktop (RDP) Client for Windows?
Step 1: Open Command Prompt
Click Win + R, type cmd, & press Enter to open the Command Prompt.
Step 2: Query User Sessions
Use the query user command to list all user sessions.
graphql Copy code
query user
Note the session ID of the user session you want to terminate.
Step 3: Logoff User Session
Use the logoff command with the session ID obtained from the previous step.
css Copy code
logoff [sessionID]
For example:
Copy code
logoff 2
Step 4: Verify Termination
Use the query user command again to verify that the session has been terminated.
graphql Copy code
query user
Managing Remote Sessions
Query Remote User Sessions
To query user sessions on a remote server, use the query user command with the /server parameter.
bash Copy code
query user /server:servername
Logoff Remote User Sessions
Similarly, use the logoff command with the /server parameter to log off a user session on a remote server.
bash Copy code
logoff [sessionID] /server:servername
Example
- Query Remote Sessions:
bash Copy code query user /server:remote_server_name
- Logoff Remote Session:
bash Copy code logoff 3 /server:remote_server_name
Advanced Management with PowerShell
PowerShell offers advanced capabilities for managing user sessions, including more complex scripts and automation.
Example PowerShell Script to Logoff User
powershell Copy code $session = (query user /server:remote_server_name | Where-Object { $_ -match 'username' }) -split ' +' logoff $session[2] /server:remote_server_name
This script identifies the session ID for the specified username on a remote server and logs off the session.
Best Practices for Managing User Sessions
- Communication: Always communicate with users before terminating their sessions to avoid data loss and disruption.
- Documentation: Keep records of session terminations for auditing and troubleshooting purposes.
- Scheduled Maintenance: Schedule regular maintenance windows to minimize the impact on users.
Conclusion
Effectively kill user sessions Windows command line is crucial for maintaining system performance, security, and stability. By leveraging command-line tools like query user, logoff, and task kill, administrators can efficiently terminate user sessions as needed. Whether working on local or remote systems, these commands provide the flexibility and control required for comprehensive session management. Remember to follow best practices and communicate with users to ensure a smooth and efficient process.
A Windows administrator can use the logoff command to log off a user session remotely from any Windows computer in the network. In this article, we’ll show how to get a list of sessions on a remote computer using the quser command and end the user session with logoff.
Using Command Prompt to Remotely Logoff Users
Before killing a user’s session in Windows, you need to get the user’s session ID. You can list sessions on the remote computer using the built-in quser console tool. Open a command prompt as an administrator and run the command:
quser /server:server_name
Note. To connect to a remote computer server_name, your account must be a member of the local Administrator group.
The quser command will list all sessions on the remote host, including the console session (SESSIONNAME=Console) and RDP user sessions (SESSIONNAME=rdp-tcp#X).
Note. You can also use the qwinsta command to get a list of user sessions on a remote computer:
qwinsta /server:server_name
Find the user in the list whose session you want to end. For example, we want to logoff the administrator session with the ID = 2.
To end a user session remotely, use the following command:
Logoff sessionID /server:ComputerName
In our example, this is:
Logoff 2 /server:server_name
Check that the user session has ended:
quser /server:server_name
If you want to log off a specific user by username, use the following PowerShell script:
$server = 'dc02' $username = 'administrator' $session = ((quser /server:$server | ? { $_ -match $username }) -split ' +')[2] logoff $session /server:$server
Possible errors when executing the logoff command:
- Could not logoff session ID 2, Error code 5
Error [5]:Access is denied.
This means that you don’t have permissions on this session or you are using a non elevated command prompt. - Error 0x00000005 enumerating sessionnames
Error [5]:Access is denied.
You are running the logoff command under a local user with administrator privileges. For such users, the Remote UAC policy is enabled by default. To disable UAC remote restrictions for local users, create the LocalAccountTokenFilterPolicy registry parameter on the target host with a value of 1.reg add "\\server_name\HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 1 /f
- Error 0x000006BA enumerating sessionnames
Error [1722]: The RPC server is unavailable.
Enable RemoteRPC on the remote machine server_name:reg add "\\server_name\HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" /v AllowRemoteRPC /t Reg_Dword /d 0x1 /f
Logoff Remote Desktop Services Users Using PowerShell
You can use the command prompt to remotely log off user sessions on Windows Server hosts with Remote Desktop Services (RDS) deployed. An RDS server can have multiple user sessions active at the same time.
If you plan to put the RDShost into maintenance mode or install updates, you need to logoff all user sessions remotely. To do this, you first need to put the RDS host in the Drain mode (in this mode, the RDS host blocks new RD connections):
Invoke-Command -ComputerName NYRDS1 ScriptBlock{ change logon /drain }
Now you can end all sessions remotely using a PowerShell script:
$Server='rds01' try { query user /server:$Server 2>&1 | select -skip 1 | % {logoff ($_ -split "\s+")[-6] /server:$Server /V} } catch {}
Or, you can only logoff RDS user sessions in a disconnected state:
$Server='rds01' try { query user /server:$Server 2>&1 | select -skip 1 | ? {($_ -split "\s+")[-5] -eq 'Disc'} | % {logoff ($_ -split "\s+")[-6] /server:$Server /V} } catch {}
Cyril Kardashevsky
I enjoy technology and developing websites. Since 2012 I’m running a few of my own websites, and share useful content on gadgets, PC administration and website promotion.
Skip to content
How to Log Off Another User in Windows 10
Although the concept of multiple users sharing one device or one PC is getting rarer by the day, there are still cases when you have to share PCs and switch users fast. Sometimes you need to end a session for another user signed in to your computer. Here are a number of methods you can use.
The situation can be different when you need to end a user session. For example, someone just forgot to sign out from his or her user account, and left running apps and opened docs, so the remain in your computer’s memory and consume its system resources. In this case, it could be useful to kick out the inactive session for the other user.
There are no third party tools required. This can be done using either Task Manager, Command Prompt or PowerShell.
- Open the Task Manager app.
- If it looks as follows, switch it to the full view using the «More details» link in the bottom right corner.
- Click on the Users tab.
- Right-click on a user account you want to log off.
- Select Sign off from the context menu.
You are done.
Alternatively, there are a couple of console tools we can use for the same purpose. Let’s review them.
Log Off Another User from the Command Prompt
- Open an elevated command prompt.
- Type or copy-paste the following command:
query session
. This will list available user sessions. - Note the ID column value for the user you want to sign off.
- Now, execute the command
logoff <ID>
. For example,logoff 1
.
You are done.
Finally, you can use PowerShell, as follows.
Log Off Another User from with PowerShell
- Open PowerShell as Administrator.Tip: You can add «Open PowerShell As Administrator» context menu.
- Type or copy-paste the following command:
$sessionID = ((quser /server:'you computer name' | Where-Object { $_ -match 'user name to sign off' }) -split ' +')[2]
- Now, execute the command
logoff $sessionID
.
The PowerShell method is great when you know the exact user name. You can save it as a script and sign out other users with one click when needed.
That’s it.
Related blog posts:
- Find the Sign Out Log in Windows 10
- All ways to sign out from Windows 10
Support us
Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:
If you like this article, please share it using the buttons below. It won’t take a lot from you, but it will help us grow. Thanks for your support!