iPhone: Apple on the Go The AngryNetworker and I Were Having a Conversation About Linux Yesterday on Facebook and I Wanted to Share My Thoughts on the Subject

Shutdown Multiple Windows Machines From The Command Prompt

Written by Saman Sadeghi on March 13, 2008 Add comments

I’ve written about my love of batch files many times and one of my favorite tricks is shutting down a computer via the command line. In this tutorial, I will show you how to shut down multiple computers, which already connected to your network, via a batch file. This particular batch file will feature a loop that calls our shut down routine as needed!

More Information

Batch files are executed line by line, from top to bottom. By using labels, we will make the program jump around a bit. This will allow us to set a variable, call central command, then reset the variable and call the central command again. By doing this, we are only creating one variable – regardless of how many computers we have - we will just reuse that single variable. Less is more people! This method creates smaller, cleaner code: We don’t need separate variables for each computer; we just reset that variable as we need! Also, it’s important to note that each computer will use the same shutdown routine.

Before we start, you will need to make sure that you know the name of each network computer and can access it. To find out the computer’s name, walk to each machine and hit the Windows Key+Pause/Break key and click on the Computer Name tab. Look for the value under “Full computer name:”. Once you know the name of each computer connected to the network (that you’d like to remotely shut down), the easiest way to see if you can access them is with the command prompt: Go to your machine (the one that will be running the command file, not the networked machines), hit the Windows Key+R, and type:
\\{computer name}
For example, if the networked computer’s name is “274D”, you would enter:
\\274D
If the computer’s shared directory is displayed, then you’re connected! If you get an error, then you need to check connectivity settings and those are beyond the scope of this article.

Creating The File

Save Text File As .batOpen Notepad and save the file, on your Desktop, as “Network Shutdown.bat” (or “I love ham”, it’s your choice :razz: ). Make sure you set the “Save as type” drop down to “All files”.

Listing Each Computer

Right-click your newly created batch file and click Edit (double-clicking it will launch the file). We need to list all of our computers in blocks. Each block be composed of three lines of code: We will set a variable with the network computer’s name, call the loop and then create a new label with that computer’s name:
set varcomputer=274D
goto loop
:274D

Let me explain what each line means:

  1. set varcomputer=274D
    By using the “set” command, we are creating a variable called “varcomputer”, we then set the variable. In this case, with the value “274D”
  2. goto loop
    We are telling the program to go to the label named “loop”, which we will create in a few steps.
  3. :274D
    This label has the value of the computer in this block. The loop below will tell the program to go to this label, then the program will continue on the next line (line by line, remember?!).

Create a block for each computer that you’d like to shutdown:
set varcomputer=274D
goto loop
:274D

set varcomputer=322A
goto loop
:322A

set varcomputer=bobs_pc
goto loop
:bobs_pc

Notice that we are reusing the varcomputer variable. After you’ve listed each computer, write the “exit” command. This will tell the computer that we are all done and it may close the progam:
exit

Creating The Loop

The loop will consist of four parts: a label, some information for the user, the actual shutdown command and the return for the loop:
:loop
echo Shutting down %varcomputer%.
shutdown -s -m \\%varcomputer% -t 600 -c "The computer is shutting down. Please save your work."
goto %varcomputer%

    Again, let’s break down each section:

  1. :loop
    This is the entrance for the loop. The second line of each computer block is sent to this label.
  2. echo Shutting down %varcomputer%.
    This is the information that is returned to the user who runs the program; it reads the current value of the “varcomputer”. This information will not be displayed on the networked machine.
  3. shutdown -s -m \\%varcomputer% -t 600 -c "The computer is shutting down. Please save all of your work."
    This is the shutdown command. It contains a few a few parts. For detailed information on each switch, please read Windows Tip: Shutdown Your Computer With The Command Prompt
  4. goto %varcomputer%
    This is the return for the loop. The program reads the current value of the “varcomputer” variable, returns to the final line of the current block and continues to the next block. By using the goto command, we are telling the program to return to a label – one that we’ve set with the “varcomputer” variable.

The Finished Product

Now that we are armed with this knowledge, we can put it all together – but before we do, let’s add a few finishing touches. At the beginning of your file, add this:
@Echo off
This will hide all commands from the user’s view and will only display comments (that are preceded by “echo”). Also, you’ll notice there are a few lines that are started with “rem”. These are internal comments that are not processed by the program. I’ve peppered the file with a couple for you to marinate on.

Your batch file should look like this:
@Echo off
cd\
Echo Saman=Program
REM ****************************
REM * Program Variables *
REM ****************************

set varcomputer=274D
goto loop
:274D

set varcomputer=322A
goto loop
:322A

set varcomputer=bobs_pc
goto loop
:bobs_pc

Exit

REM ****************************
REM * Program *
REM ****************************

:loop
echo Shutting down %varcomputer%.
shutdown -s -m \\%varcomputer% -t 600 -c “The computer is shutting down. Please save your work.”
goto %varcomputer%

Cancel The Shut Down

You can cancel a shut down that is in progess with the following line. This line cannot be run through the network! You have to walk to each machine and run the command either from the command prompt or the Run dialog, or Windows Key+R:
Shutdown -a

Start Slide Show with PicLens Lite PicLens
This post is filed in: Command Prompt, Technology, Windows Tips And Tricks and has been viewed 2,151 views times.

iPhone: Apple on the Go The AngryNetworker and I Were Having a Conversation About Linux Yesterday on Facebook and I Wanted to Share My Thoughts on the Subject

MMMMM... Related Posts


Homer
  • Windows Tip: Shutdown Your Computer With The Command Prompt
  • Windows Tip: Log Off Your Computer With A Shortcut
  • Windows Tip: Shutdown Your Computer With A Batch File
  • Customize The Command Prompt
  • Windows Hacks: Improve The Context Menu, Part 4


  • RSS feed | Trackback URI

    17 Comments »

    Comment by Kevin Doyle 2008-03-14 09:41:30

    MyAvatars 0.2

    Great little batch program and a great idea. Thanks.

    Comment by Saman Sadeghi 2008-03-16 16:57:53

    MyAvatars 0.2

    You’re welcome!

     
     
    Comment by digitalnomad 2008-03-20 14:18:16

    MyAvatars 0.2

    I had to stumble this one. Good deal.

     
    Comment by ms danielle 2008-03-20 17:58:25

    MyAvatars 0.2

    very useful, saman! :) i didn’t even know you could do this…

     
    Comment by DFragmentor 2008-03-21 10:54:20

    MyAvatars 0.2

    also, shutdown -i

    add all the computers and presto.

    Comment by Saman Sadeghi 2008-03-21 11:52:30

    MyAvatars 0.2

    You’re absolutely correct, you could use a GUI. Though, I wrote this so someone could add it to a larger, more complex batch file.

    Thanks for the tip though! :grin:

     
     
    Comment by Wong Bater 2008-03-21 11:20:08

    MyAvatars 0.2

    strange variable “loop” i was thinking your creating a loop or run until a certain point… id use “execute” instead..

     
    Comment by max connely 2008-05-06 15:04:45

    MyAvatars 0.2

    how do i see what network i am connected to and how can i connect to the same network as someone in my household. thank you

    Comment by Saman Sadeghi 2008-05-06 19:26:15

    MyAvatars 0.2

    :shock:

    I need some more information here! I’m assuming that you are connecting to the network wirelessly (if you are hardwired, then you’re on the same network). Do you have access to your wireless router’s config page? If so, look for the DHCP client table (I’ll also assume that you’re not using manual configuration for you IP Address).

    Or, you could just look at your Network Connections and read the network’s name…

     
     
    Comment by Chicken Noodle Soup 2008-05-24 19:32:25

    MyAvatars 0.2

    Wow this is neat! :grin:

    Thank you!

     
    Comment by evan 2008-07-27 08:52:16

    MyAvatars 0.2

    thanks for posting this.i like the way you write it step by step.thanks a lot.!!!!!!!

     
    Comment by evan 2008-07-27 08:55:57

    MyAvatars 0.2

    i can now shutdown all computer in the internet cafes.thanks a lot!! :twisted: hehe

     
    Comment by jho 2008-08-15 06:52:08

     
    Comment by jho 2008-08-15 06:58:12

    MyAvatars 0.2

    Can we use here the command line? :?:

    Comment by Saman Sadeghi 2008-08-15 08:31:05

    MyAvatars 0.2

    You can use the command line, though you can only pass one command at a time. If you create a batch file, you can pass as many commands as you want.

     
     
    Comment by Timothy 2008-09-25 05:07:12

    MyAvatars 0.2

    :eek: good now come up with a way to autorun this batch file without copying it to the startup.

    Comment by Saman Sadeghi 2008-09-25 07:48:59

     
     

    Leave a Reply

    Name (required)
    E-mail (required - never shown publicly)
    URI
    Your Comment (smaller size | larger size)
    You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.
    iPhone: Apple on the Go The AngryNetworker and I Were Having a Conversation About Linux Yesterday on Facebook and I Wanted to Share My Thoughts on the Subject