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

What I am going to write is already published multiple times on the Internet, but I’d like to keep the knowledge that I use on my own blog. After all, Top Secret Files! is to serve as a reference for all, including me.

All of us know how to use Start Menu to shut down, restart or hibernate or computers or log out of our user accounts. But not all of us know how to do so via a shortcut, from Command Prompt, from Windows PowerShell, from within a batch file or from within a script. In this blog post, I’d like to introduce you to Shutdown.exe and its ability to perform said actions via a shortcut, from Command Prompt or from within a batch file.

Shutdown.exe

Shutdown notice on Windows 7 Shutdown.exe is a dedicated program for performing shutdown, restart and logout that comes with Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008 and Windows 7. (It is also available on Windows 2000 Resource Kit disc.) It can be used to shut down or restart remote computers as well. It can also make Windows to shut down or restart either immediately or after a countdown timer expires.

Unfortunately shutdown.exe cannot hibernate computers on Windows XP or earlier. Moreover, Shutdown.exe cannot put the computer into sleep mode (also known as standby mode). Most importantly however, Shutdown.exe in Windows XP requires administrative privileges to shutdown or restart local machine! By default, guests and standard users who can normally shut down or restart local computer via Start Menu or other means 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

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 shortcuts to shutdown.exe on your desktop (or anywhere else) that shutdown, restart or hibernate your computer or log you out of the current user account.

To so:

  1. Go to your desktop or any folder in which you’d like to create a shortcut. You can press Windows key+D on your keyboard to reveal your desktop immediately.
  2. Bring up the context-menu. To do so, right-click on an empty space on your desktop or the folder in which you’d like to place the shortcut. Alternatively, press CTRL+Space to deselect any selected item and then press Context-Menu key on your keyboard. A menu should appear.
  3. From the menu, select “New” and then select “Shortcut”.
  4. Enter one of the commands specified in the Supported Actions table below 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 Next key.
  6. Enter a name for the shortcut. In case of example above, enter: Restart computer.
  7. Press 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 shutdown.exe command further below. You can create shortcuts that shut down local machine after a delay, shut down a remote machine (provided that you have enough privileges on it), etc.

Performing shutdown from Command Prompt and batch files

To shut down, restart or hibernate your computer or log out of your user account from 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.

Performing shutdown from Windows PowerShell and scripts

It is possible to enter the same shutdown.exe command that you’d enter in Command Prompt in Windows PowerShell command line or scripts and get the same result. However, Windows PowerShell has dedicated “cmdlets” (internal commands) that can do such a job. I will write about these command in my next blog post.

Also in case of Windows Scripting Host, you can write VBScripts or JScripts that run shutdown.exe and pass necessary parameters to it. However, I will also not explore this avenue because in my next blog post I’ll be teaching you how to use scripts to shutdown Windows without using shutdown.exe.

Supported actions

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

Action Command in Windows XP Command in Windows 7
Immediately shutdown local computer shutdown.exe -s -t 0 shutdown /s /t 0
— or —
shutdown /p
Immediately restart local computer shutdown.exe -r -t 0 shutdown /r /t 0
Immediately put the local computer to sleep (Not supported) (Not supported)
Immediately hibernate the local computer (Not supported) shutdown.exe /h
Immediately log out of the current user account shutdown.exe -l 0 shutdown.exe /l

Syntax

I have included the syntax of both Windows XP’s shutdown.exe and Windows 7’s shutdown.exe for you to compare.

Here is the shutdown.exe 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)

Here is the shutdown.exe 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 blog post, I’ll about other avenues of automated shutdown via Windows Scripts and Windows PowerShell.

Advertisements

Posted on 7 September 2010, in Windows Administration and tagged , , , , , , , , , , , , . Bookmark the permalink. 1 Comment.

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: