Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 725
Keng Avatar asked Oct 02 '08 13:10

Keng


People also ask

How do you shutdown or Restart the computer with a batch file?

type shutdown, followed by the option you wish to execute. To shut down your computer, type shutdown /s. To restart your computer, type shutdown /r. To log off your computer type shutdown /l.


2 Answers

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.

  • The -f option from JosephStyons
  • Using rundll32 from VonC
  • The Run box from Dean
  • Remote shutdown from Kip
like image 113
Keng Avatar answered Sep 28 '22 10:09

Keng


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.

like image 21
JosephStyons Avatar answered Sep 28 '22 08:09

JosephStyons