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

  • Microsoft Releases Windows 10 Version 1909

    Microsoft on Tuesday announced the release of Windows 10 version 1909, a new operating system product that's also known as the "Windows 10 November 2019 Update."

  • November Microsoft Security Bundle Addresses 75 Vulnerabilities

    Of that number, 13 vulnerabilities are rated "Critical" to patch, while 62 vulnerabilities are deemed "Important."

  • The Future of Office 365 Pricing

    With a raft of new Office 365 features in the pipeline, Microsoft also seems ready to change the way it bills its subscribers. Will it replicate Azure's pay-per-use model, or will it look like something else entirely?

  • Microsoft Offers 1 Year of Free Windows 7 Extended Security Updates to E5 Licensees

    Microsoft is offering one year of free support under its Extended Security Updates program to Windows 7 users if their organizations have E5 licensing.

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.