Restart or Shutdown Windows from a Batch File

Some time ago I wrote a post title Automating Computer Maintenance where I talked about creating batch files to execute some anti-virus, anti-spyware and a defrag application on a computer. The batch files can then be scheduled to run on a computer at a specific time to ensure that regular maintenance is performed without having a user remembering to do it.

After the post, I received e-mails about having the batch files also shutdown the computer once the maintenance was completed. The good news is that getting a batch file to shutdown, or even just restart, a computer running Windows is very easy to do. Let’s look at how this is done.

The Shutdown Command

Included in Windows XP, 2003 and Vista (I’m not sure about earlier versions of Windows), there is a file called shutdown.exe. This file allows you to shutdown or restart Windows from a command line, or batch file. It also allows you to shutdown remote machines if you have the proper access, but for the purposes of this post, I’ll stick with local machines.

Similar to many other commands, you can get a list of accepted arguments. To get this list, simply type the following at a command prompt:

shutdown.exe

While there are several arguments, the two that we are more concerned with for this post are the -s and -r arguments.

The -s argument performs a complete shutdown of the computer. The -r will log off and exit Windows, and then restart the computer. When using either of these arguments you may be presented with a countdown before the actual action is taken.

To shutdown the computer, you should use the following command:

shutdown -s

To restart the computer, you should use the following command:

shutdown -r

With regards to batch files that run scheduled maintenance, your batch file would look like:

@echo off
start /w [your anti-virus software]
start /w [your spyware software]
start /w [your defrag software]
shutdown -s

The start /w commands opens up a new command window to execute the command, while at the same time waiting for the command to complete. This is important, since you want to wait for all the necessary maintenance to complete before shutting down the computer. Once all the applications have finished executing, the computer is then shutdown.

Related Posts

What are Batch Files?

2 Responses to “Restart or Shutdown Windows from a Batch File”

  1. JAB_au says:

    I also find the -f switch handy as well:
    shutdown -r -f

    For those stubborn programs that don’t want to close properly when a shutdown occurs.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.