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


'close the text file
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

With more than fifteen years of IT experience, Don Jones is one of the world’s leading experts on the Microsoft business technology platform. He’s the author of more than 35 books, including Windows PowerShell: TFM, Windows Administrator’s Scripting Toolkit, VBScript WMI and ADSI Unleashed, PHP-Nuke Garage, Special Edition Using Commerce Server 2002, Definitive Guide to SQL Server Performance Optimization, and many more. Don is a top-rated and in-demand speaker and serves on the advisory board for TechMentor. He is an accomplished IT journalist with features and monthly columns in Microsoft TechNet Magazine, Redmond Magazine, and on Web sites such as TechTarget and Don is also a multiple-year recipient of Microsoft’s prestigious Most Valuable Professional (MVP) Award, and is the Editor-in-Chief for Realtime Publishers.

comments powered by Disqus

Reader Comments:

Thu, Apr 8, 2010 Kostas Greece

Thank you very much my friend!!!!

Wed, Feb 4, 2009 valliram India

Will this script work for vista systems also

Mon, Jun 2, 2008 Renier South Africa

Very usefull. In our country we have a severe power shortage with regular scheduled load shedding. I am looking forward to finetuning this script so that I can shutdown all our clients prior to the scheduled load shedding occurring. Needed this starting point to kick off, Thanks. Now I wonder how to add a WOL feature.

Fri, May 30, 2008 santhosh Bangalore


I have ran this script and I got the error below:

Error: RPC server is unavailable
Code: 800706BA
Source: SWbemLocator

It works fine if I have given only one Pc Name in The clients.txt,
Can I expect any help from you?

Thank you,

Sat, Jan 19, 2008 Anonymous Anonymous

I need a script that works on servers tha are not in a domain enviornment.

Wed, Dec 12, 2007 Anonymous Anonymous

Have tried this several times over the last months - always gives "invalid parameter on line 21 every time, and on every PC. I must be jinxed!

Sat, May 26, 2007 Paul Michigan

ugvdfox from UK: You could achieve this in 2 ways
One way would be to add the line 'on error resume next' - not my favorite by the way.
The 2nd way would be to have the script ping the machine and collect wether or not it is up...
here is example code:

if PingPC(pcname here) then
Do code of your choice
end if
Function PingPC(strComputer)
On Error Resume Next

Dim objShell, objPing
Dim strText

Set objShell = WScript.CreateObject("WScript.Shell")
Set objPing = objShell.Exec("cmd /c ping -n 2 " & strComputer)

strText = ""
Do While Not objPing.StdOut.AtEndOfStream
strText = strText & objPing.StdOut.ReadLine()

If Instr(strText, "Reply from") > 0 Then
PingPC = True
PingPC = False
End If
End Function

Tue, Mar 13, 2007 ugvdfox UK

Excellent script. One question however. Is it possible to tell the script if it encounters a problem with one particular workstation (i.e. because it is already powered off) to just carry on to the next as it doesn't seem to do so as it stands (just gives a vbscript error and stops)

Wed, Nov 15, 2006 T UK

I have a batch file on the server which runs:
shutdown -s -t 120
...and run that as a scheduled task on each PC; gives me the option to only execute shutdown if PC has been idle for 10 minutes.

Thu, Aug 10, 2006 Ryan Canada

Great Script . but if you don't have the WMI installed . is there some way to script it to install it remotely? and run ?

Sat, Oct 22, 2005 Anonymous Anonymous

you are da man! I can now reboot all computers with one clikc.

Tue, Oct 18, 2005 Naj Anonymous

Hi, i have a small situation at hand. I am the system admin in a college and we are planning to have lab booking for the students. I have developed a s/w in .net for the booking part. the students can book a computers in any lab for 1 hr. But the main problem is to logoff the student at the end of every hour irrespective of the time of login and restrict his account to login to this lab for the next 1 hr. I know its a bit tricky, but i was wondering if there was any script to do this.

thanks a million.


Fri, Oct 14, 2005 Cameron Evenson Manitoba, Canada

How do I reboot a domain controller after a power failure, currently the server comes up to login but no further. And this function would be nice to have in place when I head out on holidays.

Thu, Sep 1, 2005 rch Anonymous

It does not work for NT Pc's if it is logged off

Sat, Aug 20, 2005 Pathart Dominica

This is great stuff

Tue, May 31, 2005 vipul india

it is excellent and works fine but when i try to connect some machine with same credentials and administrator ID and password it gives error "RPC server is unavailable". I checked DCOM and WMI configuration. Machine are running windows 2000 professional running. reply me as soon as possible.

Sun, May 1, 2005 Brandon Anonymous

How do I use this script to log off a user from a domain controller? So far I can only shutdown or reboot a domain controller. I'm trying to prevent multiple concurrent logons so that is why I need to just logoff a user. Please respond by email if possible.

Thu, Apr 21, 2005 Dany Lebanon


Wed, Mar 9, 2005 chris UK

Nice Script!!!!

Mon, Aug 23, 2004 Shahid Abu Dhabi

It works

Fri, Jun 25, 2004 Dylan San Diego

I would be stoked if someone knew a solution to the "RPC server is unavailable" error. Otherwise this looks like it has promise for my application.

Wed, Jun 9, 2004 Scott Anonymous

How would I go about trapping that error on line 22 that occurs if a particular computer is already turned off? I want the script to continue instead of stopping on that error.

Thank you,

Scott Spencer

Wed, Jun 9, 2004 Scott Spencer Fresno, CA

Great help! The only thing I found missing was trapping for the error that occurs when a machine is already shut down (the script stops running).

Thank you,

Scott Spencer

Tue, Jun 8, 2004 Anonymous Anonymous

Excellent resource!

Sat, Jun 5, 2004 Anonymous Anonymous

That's the power of script. Congratulations.

Thu, May 27, 2004 nifnif Anonymous

i got error in line 21, char 2, code 80070005.

Sat, May 8, 2004 kumuku chennai

it's marvelous.i would like to know how to query computer names from Active Directory or NT Domain in this script

Sat, May 8, 2004 kumuku chennai

it's marvelous.

Wed, Apr 28, 2004 Anonymous Egypt

For Mr. "For the others that don't approve" I say:
Before you talk about real networks, you should know what one is! Also, you should understand the story behind that "RPC Server" thingy. Here you go...
1- RPC Server here means the presense of a listening Server Service on the target machine.
2- Microsoft Windows does not install that service unless File and Printer sharing is enabled.
3- When sharing is enabled, administrative shares are forcibly open, which means that any stolen admin credentials will cause you you job. Would you love that?
4- Why for God's sake would any body need file and printer sharing on all machines? This is too much vulnerable!
5- The Microsoft way to do it - unfortunately will not work except for a fixed time, otherwise, you can manage it with running script in the background - is to make a startup script and assign it using group policy for whatever level you need. This is the most reasonable.

Mon, Apr 26, 2004 JD Hacker

It's wonderful ... more and more ideas ...

Thu, Apr 22, 2004 Anonymous Anonymous

Excellent. This puts us in the "Hand of God" mode.

Fri, Apr 9, 2004 Emil Polans

Very Useful !

Thu, Apr 8, 2004 Anonymous Anonymous

works great, I'm lovin' it

Fri, Apr 2, 2004 Dan denver

Works sweet and I have been looking for something that will do this for quite some time. Great work!

For the others that don't approve, get a real network and then the script will work...

Thu, Apr 1, 2004 Anonymous Egypt

Wouldn't work in workgroup mode (Mixed Environments). It would give error "The RPC Server is Unavailable", although the correct credentials are given! Besides, it is easier to use the shutdown program within a batch. :-)

Thu, Apr 1, 2004 Anonymous Egypt

Wouldn't work in workgroup mode. It would give error "The RPC Server is Unavailable", although the correct credentials are given! Besides, it is easier to use the shutdown program within a batch. :-)

Wed, Mar 31, 2004 Anonymous Anonymous

Perfect script, especially in combination with a UPS in case of power failure

Tue, Mar 30, 2004 Anonymous Anonymous

Very usefull

Mon, Mar 29, 2004 Anonymous Anonymous

It is a great solution
Iqbal Shaikh

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Please type the letters/numbers you see above

Redmond Tech Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.