How do I shutdown, restart, or log off Windows via a bat file?

2,418,474

Solution 1

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 -h means "help" (which it does for every other command-line program... except shutdown.exe, where it means "hibernate"). They then run shutdown -h and 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 0 to 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.

Solution 2

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.

Brutus 2006 is a utility that provides a GUI for these options.

Solution 3

No one has mentioned -m option for remote shutdown:

shutdown -r -f -m \\machinename

Also:

  • The -r parameter causes a reboot (which is usually what you want on a remote machine, since physically starting it might be difficult).
  • The -f parameter option forces the reboot.
  • You must have appropriate privileges to shut down the remote machine, of course.

Solution 4

Original answer: Oct. 2008

You also got all the "rundll32.exe shell32.dll" serie:

(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
    

where n stands for:

  • 0 - LOGOFF
  • 1 - SHUTDOWN
  • 2 - REBOOT
  • 4 - FORCE
  • 8 - POWEROFF

(can be combined -> 6 = 2+4 FORCE REBOOT)


Update April 2015 (6+ years later):

1800 INFORMATION kindly points out in the comments:

Don't use rundll32.exe for 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 ExitWindows.

Raymond CHEN wrote:

The function signature required for functions called by rundll32.exe is:

void CALLBACK ExitWindowsEx(HWND hwnd, HINSTANCE hinst,
       LPSTR pszCmdLine, int nCmdShow);

That hasn't stopped people from using rundll32 to call random functions that weren't designed to be called by rundll32, like user32 LockWorkStation or user32 ExitWindowsEx.

(oops)

The actual function signature for ExitWindowsEx is:

BOOL WINAPI ExitWindowsEx(UINT uFlags, DWORD dwReserved);

And to make it crystal-clear:

Rundll32 is a leftover from Windows 95, and it has been deprecated since at least Windows Vista because it violates a lot of modern engineering guidelines.

Solution 5

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.

Share:
2,418,474
Keng
Author by

Keng

Personal Achievables: Medical Note: I now have irreversible damage to my kidneys and liver after having used this site.

Updated on March 30, 2021

Comments

  • Keng
    Keng about 3 years

    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?

  • Keng
    Keng over 15 years
    yep...I just like to have the double-click sitting there...that's what the -r bat file is for too ;o)
  • Dean Rather
    Dean Rather over 15 years
    careful not to put it in programs->startup :)
  • Asdfg
    Asdfg almost 12 years
    whats the difference between shutdown and poweroff?
  • VonC
    VonC almost 12 years
    @Asdfg today? none. For NT4 and before, the motherboard didn't always support power off, only shutdown (windows was completely closed, but the PC was still under electric power). Only poweroff (if the hardware supported it at the time) closed windows and cut the electic power. Today, both shutdown and poweroff do that.
  • pasx
    pasx over 10 years
    Do read about using -f (for force) in the next answer. Says the guy who didn't and now has a computer stuck on shutdown on the other side of the planet during the weekend (:
  • rsk82
    rsk82 over 10 years
    you can put pause in the bath file, and then abort command, such the abort sequence would be any key
  • Zitrax
    Zitrax over 10 years
    Reading the help for shutdown on Windows 8.1 I see: "If the timeout period is greater than 0, the /f parameter is implied." And as the default timeout is 30 seconds I think it's preferable to give some time for the clean shutdown and then the forced shutdown happens after the timeout.
  • legends2k
    legends2k about 10 years
    +1 just that I used /<switch> instead of -<switch>. The help section also uses /.
  • Gavin
    Gavin almost 10 years
    @rsk82, well being able to get away with typing anything was an unfortunate limitation of my TRS-80 copy of Oregon Trail too, but frankly I always considered it cheating and my brother would get enraged whenever I did it. Just saying: why would you want to enrage my brother?
  • JosephStyons
    JosephStyons over 9 years
    That's a good point. I believe when I wrote this, the OS I was using was Windows XP. I see that the "/f is implied" remark is in Windows 7 as well. I've modified the example accordingly.
  • Heri
    Heri over 9 years
    My experience: Using the -s option (shutdown) in Remote Desktop (as the OP mentions) only terminates the remote desktop but leaves the remote machine untouched.
  • 1800 INFORMATION
    1800 INFORMATION about 9 years
    Don't use rundll32.exe for 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 ExitWindows.
  • 1800 INFORMATION
    1800 INFORMATION about 9 years
  • VonC
    VonC about 9 years
    @1800INFORMATION very good point. I have rewritten the answer to make it clear this is not a valid option.
  • dzampino
    dzampino over 8 years
    By default they have 30 seconds. I use for immediate: shutdown /r /t 0 and for friendly: shutdown /r
  • PRMan
    PRMan over 8 years
    To be fair, though, it really doesn't matter what type of memory leak or stack corruption you are causing if you are immediately shutting down the PC.
  • granadaCoder
    granadaCoder about 8 years
    I use "-r" for Remote Machines. So it will restart and not need manual intervention.
  • mcmillab
    mcmillab almost 8 years
    can the machine name be an IP address? Ie, can you do this over the internet?
  • Brethlosze
    Brethlosze almost 8 years
    aw.. and curious behavior will be obtained if the code is placed in a shutdown.bat file. Try it! :)....
  • user324747
    user324747 almost 8 years
    I used to use that method but it doesn't work anymore, command line it is.
  • user324747
    user324747 almost 8 years
    I also have a gadget which has buttons for shutdown, restart, hibernate, lock that works in RDP.
  • Timothy Zorn
    Timothy Zorn over 7 years
    If you can access the machine via \\IP.AD.DR.ESS (i.e. the necessary ports are open and you have the required permissions), then yes, this should work over the internet.
  • Ole V.V.
    Ole V.V. over 7 years
    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. 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. The -f is implied by the timeout. (from proposed edit by Mateusz Konieczny)
  • Dchris
    Dchris about 7 years
    I first tried with -r only and it didn't work because i wanted a remote shutdown. It worked with shutdown -r -m. -f was not needed.
  • Kingzel
    Kingzel about 6 years
    I would like to point out flaws in this answer first of all ` shutdown -i ` is not for filling in functions or any of that jazz. I am sure that it is used to remotely shutdown other computers in the currently connected LAN and shutdown -t xxis not a stand alone command so you have to use with shutdown -s or shutdown -r
  • msoft
    msoft almost 6 years
    @Keng, reading the actual problem "I'm not able to use the shutdown/restart function in the Start menu" it appears to me that you want to know how to do that. The command-line is one solution, the keyboard is another. It might help other users who stumble onto this question and just want an easy solution to shutdown or restart.