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 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.
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.
- 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.
- 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.
- From the menu, select “New” and then select “Shortcut”.
- 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.
- Press Next key.
- Enter a name for the shortcut. In case of example above, enter: Restart computer.
- 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.
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 —
|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|
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):
Here is the shutdown.exe syntax in Windows 7 (modified a bit to fit this blog post):
In the next blog post, I’ll about other avenues of automated shutdown via Windows Scripts and Windows PowerShell.
Posted on 7 September 2010, in Windows Administration and tagged administrative privileges, hibernate, keyboard, lock computer, logoff, power off, restart, script, shutdown, shutdown.exe, sleep, switch user, Windows. Bookmark the permalink. 1 Comment.