Shutting down, restarting or logging out via shortcuts, batch files and scripts – Part 1

All of us know how to use the Start menu to shut down, restart or hibernate our computers, or log out of our user accounts. But not all of us know how to do so via a shortcut, from the command line, or from within a script or batch file. In this post, I’d like to introduce you to Shutdown.exe.

Shutdown.exe

Shutdown notice on Windows 7: It appears after Shutdown.exe is executed.

Shutdown.exe is a dedicated program for performing shutdown, restart and logout. It comes with Windows XP and later. (It is also available on Windows 2000 Resource Kit disc.) It can schedule its action to be done after a certain period of time and works for remote computers too.

Unfortunately Shutdown.exe cannot hibernate computers on Windows XP or earlier. Moreover, it cannot put the computer into the sleep mode (also known as the standby mode). Most importantly, however, in Windows XP, it requires administrative privileges to shut down or restart a local machine. By default, guests and standard users who can normally shut down or restart local computer via the Start menu cannot do so via Shutdown.exe. If they try to do so, they will encounter the following cryptic error and misleading message:

Operation completed successfully.
A required privilege is not held by the client.

Shutdown notice on Windows XP: It appears after Shutdown.exe is executed.

The default behavior of this command in Windows XP can be modified by editing a local security policy item called “Force shutdown from a remote system” and suffering the consequences of doing so!

Fortunately in Windows 7, Shutdown.exe can, by default, shutdown or restart without requiring administrative privileges or unusual user rights in the security policy.

Creating shortcuts that perform shutdown

You can create a shortcut to Shutdown.exe on your desktop (or anywhere else). To so:

  1. Reveal your desktop (or open the folder in which you intend to create a shortcut.) You can press the Windows key+D key combination on your keyboard to reveal your desktop immediately.
  2. Bring up the context menu. To do so, right-click on an empty space. Alternatively, press CTRL+Space to deselect any selected item and then press the Context Menu key on your keyboard. A menu should appear.
  3. From the menu, select “New” and then select “Shortcut”.
  4. Supply one of the command strings specified in the “Supported actions“. Choose one that corresponds to the purpose of your shortcut. For example, if you are creating a shortcut to restart your Windows XP computer, enter: shutdown.exe -r -t 0.
  5. Press the Next key.
  6. Enter a name for the shortcut. In the case of the example above, enter: Restart computer.
  7. Press the Finish key.

You can create more complex shortcut by customizing the command that you entered in step 4. To do so, study the syntax of the Shutdown.exe command below. You can create shortcuts that stop the local machine after a delay, or stop a remote machine (provided that you have enough privileges on it.)

Performing shutdown from the command line or batch files

To shut down, restart or hibernate your computer, or log out of your user account from the Command Prompt, just enter a valid shutdown.exe command (like the one included in table below) and press ENTER. It will behave just like the shortcut. Same command can be entered in a batch file to produce the same effect.

It is possible to run Shutdown.exe via PowerShell or Windows Scripting Host too, but it is pointless and inconvenient. After all, PowerShell has its own dedicated cmdlets for doing the job, and Windows Scripting Host has its own native ways.

Supported actions

The table below lists the supported actions by shutdown.exe:

ActionCommand in Windows XPCommand in Windows 7
Stop the local computer nowshutdown.exe -s -t 0shutdown /s /t 0
— or —
shutdown /p
Restart the local computer nowshutdown.exe -r -t 0shutdown /r /t 0
Put the local computer to sleep now(Not supported)(Not supported)
Hibernate the local computer now(Not supported)shutdown.exe /h
Log out of the current user account nowshutdown.exe -l 0shutdown.exe /l

Syntax

Here is the Shutdown.exe’s syntax in Windows XP (modified a bit to fit this blog post):

Usage: shutdown [-i | -l | -s | -r | -a] [-f] [-m \computer] [-t xx] [-c "comment"] [-d up:xx:yy]
  No args             Display this message (same as -?)
     -i               Display GUI interface, must be the first option
     -l               Log off (cannot be used with -m option)
     -s               Shutdown the computer
     -r               Shutdown and restart the computer
     -a               Abort a system shutdown
     -m \computer     Remote computer to shutdown/restart/abort
     -t xx            Set timeout for shutdown to xx seconds
     -c "comment"     Shutdown comment (maximum of 127 characters)
     -f               Forces running applications to close without warning
     -d [u][p]:xx:yy  The reason code for the shutdown
                      u is the user code
                      p is a planned shutdown code
                      xx is the major reason code (positive integer less than 256)
                      yy is the minor reason code (positive integer less than 65536)

And here is the Shutdown.exe’s syntax in Windows 7 (modified a bit to fit this blog post):

Usage: shutdown [/i | /l | /s | /r | /g | /a | /p | /h | /e] [/f]
    [/m \\computer][/t xxx][/d [p|u:]xx:yy [/c "comment"]]

    No args    Display help. This is the same as typing /?.
    /?         Display help. This is the same as not typing any options.
    /i         Display the graphical user interface (GUI).
               This must be the first option.
    /l         Log off. This cannot be used with /m or /d options.
    /s         Shutdown the computer.
    /r         Shutdown and restart the computer.
    /g         Shutdown and restart the computer. After the system is
               rebooted, restart any registered applications.
    /a         Abort a system shutdown.
               This can only be used during the time-out period.
    /p         Turn off the local computer with no time-out or warning.
               Can be used with /d and /f options.
    /h         Hibernate the local computer.
               Can be used with the /f option.
    /e         Document the reason for an unexpected shutdown of a computer.
    /m \\computer Specify the target computer.
    /t xxx     Set the time-out period before shutdown to xxx seconds.
               The valid range is 0-315360000 (10 years), with a default of 30.
               If the timeout period is greater than 0, the /f parameter is
               implied.
    /c "comment" Comment on the reason for the restart or shutdown.
               Maximum of 512 characters allowed.
    /f         Force running applications to close without forewarning users.
               The /f parameter is implied when a value greater than 0 is
               specified for the /t parameter.
    /d [p|u:]xx:yy  Provide the reason for the restart or shutdown.
               p indicates that the restart or shutdown is planned.
               u indicates that the reason is user defined.
               If neither p nor u is specified the restart or shutdown is
               unplanned.
               xx is the major reason number (positive integer less than 256).
               yy is the minor reason number (positive integer less than 65536).

Reasons on this computer:
(E = Expected U = Unexpected P = planned, C = customer defined)
Type    Major   Minor   Title

 U      0       0       Other (Unplanned)
E       0       0       Other (Unplanned)
E P     0       0       Other (Planned)
 U      0       5       Other Failure: System Unresponsive
E       1       1       Hardware: Maintenance (Unplanned)
E P     1       1       Hardware: Maintenance (Planned)
E       1       2       Hardware: Installation (Unplanned)
E P     1       2       Hardware: Installation (Planned)
E       2       2       Operating System: Recovery (Planned)
E P     2       2       Operating System: Recovery (Planned)
  P     2       3       Operating System: Upgrade (Planned)
E       2       4       Operating System: Reconfiguration (Unplanned)
E P     2       4       Operating System: Reconfiguration (Planned)
  P     2       16      Operating System: Service pack (Planned)
        2       17      Operating System: Hot fix (Unplanned)
  P     2       17      Operating System: Hot fix (Planned)
        2       18      Operating System: Security fix (Unplanned)
  P     2       18      Operating System: Security fix (Planned)
E       4       1       Application: Maintenance (Unplanned)
E P     4       1       Application: Maintenance (Planned)
E P     4       2       Application: Installation (Planned)
E       4       5       Application: Unresponsive
E       4       6       Application: Unstable
 U      5       15      System Failure: Stop error
 U      5       19      Security issue
E       5       19      Security issue
E P     5       19      Security issue
E       5       20      Loss of network connectivity (Unplanned)
 U      6       11      Power Failure: Cord Unplugged
 U      6       12      Power Failure: Environment
  P     7       0       Legacy API shutdown

Stay tuned

In the next post, I’ll write about other avenues of automated shutdown via Windows Scripting Host and PowerShell.

One thought on “Shutting down, restarting or logging out via shortcuts, batch files and scripts – Part 1

Leave a comment (Markdown syntax accepted)

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