PowershellGuru
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login
No Result
View All Result
PowershellGuru
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login
No Result
View All Result
PowershellGuru
No Result
View All Result
Home Powershell Script blogs

How to Log off Users Remotely with PowerShell (2022)

themeanmachine19@gmail.com by [email protected]
March 17, 2022
in Powershell Script blogs
0
Log off Users Remotely with PowerShell
Share on FacebookShare on Twitter

You might also like

Add extra dns server using powershell

How To Add Extra DNS Server Using PowerShell (2022)

July 7, 2022
Instant guide to convert ps2 to exe

Instant Guide To Convert PS1 To EXE (2022)

May 21, 2022

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 users remotely with 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

close

DON’T MISS A POST

Keep up to date with PowershellGuru

Powershell Blogs

PowershellGuru provides the best PowerShell scripts available that can be used and download freely. Do Check our blogs to get updated regularly.

Check your inbox or spam folder to confirm your subscription.

themeanmachine19@gmail.com

[email protected]

Related Stories

Add extra dns server using powershell

How To Add Extra DNS Server Using PowerShell (2022)

by [email protected]
July 7, 2022
0

Add extra DNS server using PowerShell. Add third DNS server IP remotely using PowerShell. Get-DnsClientServerAddress. Set-DnsClientServerAddress.

Instant guide to convert ps2 to exe

Instant Guide To Convert PS1 To EXE (2022)

by [email protected]
May 21, 2022
0

Instant guide to convert ps1 to exe. How to convert ps1 to exe. PS2exe. Convert ps1 to exe using ps2exe.

PowerShell tips and tricks

5 Useful PowerShell Tips and Tricks

by [email protected]
May 15, 2022
0

Useful Powershell tips and tricks. PowerShell tips and tricks. Know Powershell tips and tricks. get-help. get-alias.

tips to manage hyper-v using powershell

5 Tips To Manage Hyper-V Using PowerShell

by [email protected]
May 1, 2022
0

Tips to manage hyper-v using PowerShell. How to manage Hyper-V using PowerShell. Powershell to manage Hyper-V. Hyper-v and PowerShell.

Next Post
Get-Eventlog: PowerShell way to query event logs

Get-Eventlog: PowerShell way to query event logs (2022)

Please login to join discussion

Recommended

Get Windows Update Status Using PowerShell

Get Windows Update Status Using PowerShell (Fast)

November 21, 2021
send-mailmessage

Send-mailmessage: Using Powershell to send email (2021)

May 27, 2021

About

Dhrub Bharali

PowerShell Enthusiast

Dhrub is hardcore Powershell enthusiast, he has wriiten more than 100 powershell scripts and he is the sole owner of PowerShellGuru.

Follow Us

Popular Story

  • Installing software remotely using powershell

    Easy way to install software remotely using PowerShell (2021)

    1059 shares
    Share 424 Tweet 265
  • How to find NTP Server using PowerShell?

    938 shares
    Share 375 Tweet 235
  • Get-LocalGroupMember: Find Local admin using PowerShell (2021)

    889 shares
    Share 356 Tweet 222
  • How to Log off Users Remotely with PowerShell (2022)

    816 shares
    Share 326 Tweet 204
  • Powershell filter: Using where-object and select-object (2021)

    799 shares
    Share 320 Tweet 200
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login

© 2022 PowershellGuru- PowerShell Scripts For Automation

No Result
View All Result
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login

© 2022 PowershellGuru- PowerShell Scripts For Automation

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.