How to Log off Users Remotely with PowerShell (2022)

Log off Users Remotely with PowerShell

PowerShell is a very powerful tool; it’s also one that many people don’t know how to use. If you’re an IT pro and are constantly in the office, you may not need to log off remotely. But if you’re a remote worker or just want to be able to log on and off your computer from any location, this post is for you! We’ve all been there. Working late at night, getting ready to head home for the day, then realizing that you need to do one more thing. Remotely logging off from your computer is a simple process when you know how,We’ll walk through 4 ways to do so using PowerShell. 

Log off User using Invoke-RDUserLogoff

As the name suggest Invoke-RDUserLogoff is used for logging off users remotely. Below is the syntax for the same.

				
					Invoke-RDUserLogoff
      [-HostServer] <String>
      [-UnifiedSessionID] <Int32>
      [-Force]
      [<CommonParameters>]
				
			

Below is one example, also in case you want to force the logoff use        -Force at the end of the below example.

				
					Invoke-RDUserLogoff -HostServer "ser1.test.com" -UnifiedSessionID 2
				
			

-UnifiedSessionID is the ID of the remote user connected to the server or workstation. Before providing any number first we need to understand how can we get one from the server.

How to identify user logged into servers?

#1 CMD way to identify users on remote servers.

Here we can use Qwinsta or Quser to find the user name and ID details of the user that we need to logoff.

				
					query user / server:remoteserver
				
			

#2  PowerShell way to identify users on remote servers

We have 3 methods the first is the same what we have used in #1 , the other 2 are as below. Both of the below query will provide the username and ID of the user connected remotely to the server.

				
					Invoke-Command -ComputerName 'REMOTECOMPUTER' -ScriptBlock { quser }
 
				
			
				
					$server   = 'servername.test.com'
$username = $env:USERNAME
$session = ((quser /server:$server | ? { $_ -match $username }) -split ' +')[1..2]
$session
				
			

Note: The above commands i.e. quser or qwinsta don’t work on workstation like Windows 8 or 10. These command only rus on the server. If you get any not recognized issue on workstation that it is predictable.

Log off users remotely with PowerShell

Now let’s understand our approach to log off users from remote servers. We will discuss CMD and multiple ways with PowerShell to do this activity.

#1 CMD way to logoff users on remote servers.

Here we can use Rwinsta to logoff the user remotely. Below is the syntax for the same. The session ID can be retrieved by using Quser/Qwinsta as mentioned above.

				
					rwinsta <SESSION_ID> /SERVER:<NAME>
				
			

# PowerShell way to logoff users on remote server

(1) We have already discussed about Invoke-RDUserLogoff , hence we will try to achieve the same using different commands.

WMI/CIM and the Win32Shutdown() function are one option. This technique does not need you to find a user session first, but it also does not allow you to choose a user. This method simply logs off all users who are currently logged on to the remote computer interactively. In PowerShell, you can do this with only one line.

I’m invoking a CIM procedure on the DC machine again and supplying 4 as a flag, as shown below. The user is forced to log off as a result of this. You can also use 0 to perform a “graceful” logout here. Read more about flags here.

				
					Invoke-CimMethod -ClassName Win32_Operatingsystem -ComputerName DC -MethodName Win32Shutdown -Arguments @{ Flags = 4 }
				
			

(2) Logoff specific user using PowerShell

				
					$userName = 'test4'
$sessionId = ((quser /server:DC | Where-Object { $_ -match $userName }) -split ' +')[2]
$sessionId
logoff $sessionId /server:DC
				
			

(3) Logoff each user on remote server using PowerShell.

				
					try {
   query user /server:$SERVER 2>&1 | select -skip 1 | foreach {
     logoff ($_ -split "\s+")[-6] /server:$SERVER
   }
}
catch {}
				
			

(4) Logoff only disconnected users on remote servers using PowerShell. We have used try and catch exception handling if you want to read more about it click here.

				
					$Servers = gc C:\listofservers.txt
foreach($Server in $Servers) {
    try {
        query user /server:$Server 2>&1 | select -skip 1 | ? {($_ -split "\s+")[-5] -eq 'Disc'} | % {logoff ($_ -split "\s+")[-6] /server:$Server /V}
    }
    catch {}
    }
				
			

Conclusion

I hope you have liked the post How to log off users remotely with PowerShell and will implement this whenever it is required. Things you should always remember is to try with one user so that it will be easy for you to make changes. Let me know if you want a blog post on some other script that might amaze you.

We are working continuously to provide you with the better and the best scripts daily. We will publish weekly hence don’t forget to subscribe to our newslette

Leave a Reply

Please disable your adblocker or whitelist this site!