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 an Author/Evangelist 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

  • RAMBleed Side-Channel Attack Method Disclosed by Researchers

    Academic researchers this week published information about another side-channel attack method, called "RAMBleed," that can expose information from memory chips, including encryption key information.

  • Penguin

    Windows 10 Preview Build 18917 Shows Off New Linux Integration

    Microsoft's latest Windows 10 "fast-ring" preview release is showcasing a coming Delivery Optimization enhancement, along with the ability to try the newly emerged Windows Subsystem for Linux version 2.

  • Customizing Microsoft Office 365

    While the overall look and feel of Office 365 is pretty standard across organizations, there are several ways to personalize it and make it fit better with your company's specific needs.

  • Microsoft 365 Business Tenants Getting Conditional Access and Trouble-Ticket Features

    Microsoft added its conditional access security service to Microsoft 365 Business subscriptions, according to a Wednesday announcement, and it also added new trouble-ticket features for Microsoft 365 administrators.

comments powered by Disqus

Office 365 Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.