Tips and Tricks

Remote Shutdown

This script allows logoff and shut down of remote users.

After my February feature, “Automate Your Security,” with VBScript, I received a lot of requests for a script that would remotely log off remote users and even remotely shut down their computers. What a great idea! So, I decided to see if it could be done with VBScript and Windows Management Instrumentation (WMI)—and it can. Following is the script you can use.

Dim oFSO, oTS, sClient, oWindows, oLocator, oConnection, oSys
Dim sUser, sPassword

'set remote credentials
sUser = "Administrator"
sPassword = "password"

'open list of client names
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTS = oFSO.OpenTextFile("C:\clients.txt")

Do Until oTS.AtEndOfStream
 
 'get next client name
 sClient = oTS.ReadLine
 
 'get WMI locator
 Set oLocator = CreateObject("WbemScripting.SWbemLocator")

 'Connect to remote WMI
 Set oConnection = oLocator.ConnectServer(sClient, _
   "root\cimv2", sUser, sPassword)

  'issue shutdown to OS
 ' 4 = force logoff
 ' 5 = force shutdown
 ' 6 = force rebooot
 ' 12 = force power off
 Set oWindows = oConnection.ExecQuery("Select " & _
   "Name From Win32_OperatingSystem")
 For Each oSys In oWindows
   oSys.Win32ShutDown(5)
 Next

Loop

'close the text file
oTS.Close
WScript.Echo "All done!"

You should know a couple of things about this script. First, you need to have a text file named C:\Clients.txt on the machine where the script runs. That file should contain the list of client names you want to shut down. Instead of using a text file, you can modify this script to use ADSI, enabling it to query computer names from Active Directory or an NT domain. However, as you probably don’t want to run this script against every computer on your network (shutting down all of your servers might be bad), the text file provides you with complete control over which computers the script will affect.

Note that I’ve hard-coded a name and password into this script. That usually isn’t necessary, provided the account you use to run the script has administrative privileges on all the clients it’ll try to shut down. Regardless, don’t leave the script lying around with an actual username and password typed in!

Automated Script Execution
This script can be scheduled, using Windows’ Task Scheduler, to execute automatically. Run it from your management workstation each evening and you’ll be sure every workstation is properly logged off, shut down or whatever.

Finally, note that this script is currently set to force a shutdown, using 5 in the call to Win32ShuDown(). As noted in the script, you can change that to 4, 6 or 12 to force a logoff, restart or power off, respectively.

This script should work with all 32-bit versions of Windows back to, and including, Windows NT 4.0 Service Pack 4. That means any NT boxes you have sitting around, along with all Windows 2000, Windows XP and Windows Server 2003 machines should respond to this script. This script relies on remote machines having the WMI service installed and running. If you’ve modified the default settings for that service and it isn’t running on your computers, then this script won’t be helpful to you.

About the Author

Don Jones is a multiple-year recipient of Microsoft’s MVP Award, and is Curriculum Director for IT Pro Content for video training company Pluralsight. Don is also a co-founder and President of PowerShell.org, a community dedicated to Microsoft’s Windows PowerShell technology. Don has more than two decades of experience in the IT industry, and specializes in the Microsoft business technology platform. He’s the author of more than 50 technology books, an accomplished IT journalist, and a sought-after speaker and instructor at conferences worldwide. Reach Don on Twitter at @concentratedDon, or on Facebook at Facebook.com/ConcentratedDon.

Featured

comments powered by Disqus

Subscribe on YouTube

Upcoming Training Events

0 AM
TechMentor @ Microsoft HQ
August 11-15, 2025