Restart or Shutdown Windows from a Batch File

  • Sharebar

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?

You may also like:

No thumbnail image

What are Batch Files?

No thumbnail image

How to Automate Your Computer Maintenance

Windows 2003 Logo

Windows Installer Fix: Error 1718. File was rejected by digital signature policy

Sleep Mode and Hibernate

Shutdown Your Computer Automatically After Any Task

2 people had something to say about “Restart or Shutdown Windows from a Batch File”:

Comments


  1. 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.


    • Good point. I use that switch as well to avoid waiting for a program.

Do you have something to say? Let everyone know!

Commenting policy: All comments are moderated for spam. You must use your real name and not your website name or keywords. If a comment is deemed to be spam, then it will be deleted or edited. Links to your website within the comment body is not permitted, but you are free to use CommentLuv to add a link to your latest post. If you wish to add a link to your website, you can always contact me about submitting a guest post.






CommentLuv badge
This blog uses premium CommentLuv which allows you to put your keywords with your name if you have had 5 approved comments. Use your real name and then @ your keywords (maximum of 3)

Previous Post:

Next Post: