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
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
Open Notepad and save the file, on your Desktop, as “Network Shutdown.bat” (or “I love ham”, it’s your choice
). 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:
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”goto loop
We are telling the program to go to the label named “loop”, which we will create in a few steps.: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:
:loop
This is the entrance for the loop. The second line of each computer block is sent to this label.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.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 Promptgoto %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