How do I shutdown, restart, or log off Windows via a bat file?
I've been using Remote Desktop Connection to get into a workstation. But in this environment, I cannot use the power options in Start Menu. I need an alternative way to shutdown or restart.
How do I control my computer's power state through the command line?
The most common ways to use the
shutdown command are:
shutdown -s— Shuts down.
shutdown -r— Restarts.
shutdown -l— Logs off.
shutdown -h— Hibernates.
Note: There is a common pitfall wherein users think
-hmeans "help" (which it does for every other command-line program... except
shutdown.exe, where it means "hibernate"). They then run
shutdown -hand accidentally turn off their computers. Watch out for that.
shutdown -i— "Interactive mode". Instead of performing an action, it displays a GUI dialog.
shutdown -a— Aborts a previous shutdown command.
The commands above can be combined with these additional options:
-f— Forces programs to exit. Prevents the shutdown process from getting stuck.
-t <seconds>— Sets the time until shutdown. Use
-t 0to shutdown immediately.
-c <message>— Adds a shutdown message. The message will end up in the Event Log.
-y— Forces a "yes" answer to all shutdown queries.
Note: This option is not documented in any official documentation. It was discovered by these StackOverflow users.
I want to make sure some other really good answers are also mentioned along with this one. Here they are in no particular order.
If you are on a remote machine, you may also want to add the -f option to force the reboot. Otherwise your session may close and a stubborn app can hang the system.
I use this whenever I want to force an immediate reboot:
shutdown -t 0 -r -f
For a more friendly "give them some time" option, you can use this:
shutdown -t 30 -r
As you can see in the comments, the -f is implied by the timeout.
Read more... Read less...
No one has mentioned
-m option for remote shutdown:
shutdown -r -f -m \\machinename
-rparameter causes a reboot (which is usually what you want on a remote machine, since physically starting it might be difficult).
-fparameter option forces the reboot.
- You must have appropriate privileges to shut down the remote machine, of course.
Original answer: Oct. 2008
You also got all the
(see update below)
rundll32.exe user.exe,**ExitWindows**[Fast Shutdown of Windows]
rundll32.exe user.exe,**ExitWindowsExec**[Restart Windows]
rundll32.exe shell32.dll,SHExitWindowsEx n
n stands for:
- 0 -
- 1 -
- 2 -
- 4 -
- 8 -
(can be combined -> 6 = 2+4
Update April 2015 (6+ years later):
rundll32.exefor this purpose. It expects that the function you passed on the command line has a very specific method signature - it doesn't match the method signature of
Raymond CHEN wrote:
The function signature required for functions called by
void CALLBACK ExitWindowsEx(HWND hwnd, HINSTANCE hinst, LPSTR pszCmdLine, int nCmdShow);
That hasn't stopped people from using
rundll32to call random functions that weren't designed to be called by
The actual function signature for ExitWindowsEx is:
BOOL WINAPI ExitWindowsEx(UINT uFlags, DWORD dwReserved);
And to make it crystal-clear:
Rundll32is a leftover from Windows 95, and it has been deprecated since at least Windows Vista because it violates a lot of modern engineering guidelines.
Another small tip: when going the batch file route, I like to be able to abort it in case I run it accidentally. So the batch file invokes the shutdown but leaves you at the command prompt afterwards.
@echo off echo Shutting down in 10 seconds. Please type "shutdown /a" to abort. cmd.exe /K shutdown /f /t 10 /r
Plus, since it's on a timer, you get about the same thrill as you do when hunting in The Oregon Trail.
You're probably aware of this, but just in case: it's much easier to just type
shutdown -r (or whatever command you like) into the "Run" box and hit enter.
Saves leaving batch files lying around everywhere.
When remoted into a machine (target is Windows XP anyway; I am not sure about target Windows Vista), although Shutdown on the start menu is replaced by Disconnect Session or something like that, there should be one called 'Windows Security' which also does the same thing as Ctrl + Alt + End as pointed to by Owen.