Windows batch: How do I shutdown/restart/logoff using a bat file?



Sometimes having a .bat file to restart/shutdown or logoff could be helpful. One common use case is when you need to restart or shutdown while using remote desktop (buttons will be disabled). In this article we will use the shutdown command that allow us to restart/shudown or logoff.

Using shutdown command

The batch file should contain a call to the shutdown command with the proper options.

For example for inmediate shutdown while connected using a remote desktop session:

shutdown -t 0 -r -f

If you want to add a 30 seconds to start the shutdown:

shutdown -t 30 -r

Here we list most of the common options for shutdown command:

  • shutdown -r — restarts
  • shutdown -s — shutsdown
  • shutdown -l — logoff
  • shutdown -t xx — where xx is number of seconds to wait till shutdown/restart/logoff
  • shutdown -i — gives you a dialog box to fill in what function you want to use
  • shutdown -a — aborts the previous shutdown command....very handy!
  • shutdown -h — hibernate.

Shutdown command on remote sessions

When you are on a remote session (remote desktop), add the -f option to force the reboot. If you don't add -f, the session may close and can hang the windows.