Shutdown or restart a computer using AutoIt

How to easily shutdown a computer using a AutoIt script.

4327 views

Edited: 2017-07-26 10:23

This simple AutoIt script will show you how to use a simple "YES and NO" MsgBox to shutdown the computer if the "YES" button is clicked in the dialog. In this script, we will be using the build-in shutdown function, which allow us to either shutdown, reboot, power-down, hibernate or make the computer go standby.

The shutdown function accepts different combinations of values, which then trigger different outcomes. If you simply want to shut down the computer, you can use a value of "1". Other values trigger different outcomes, and the values can even be added together. Here's just a few.

Shut down:

Shutdown(1)

Reboot:

Shutdown(2)

Forcefully Reboot:

Shutdown(6)

Power off the computer:

Shutdown(8)

Sets system to standby:

Shutdown(32)

Note. You can read more about the accepted values here.

Prompt the user to restart the computer

To prompt the user to restart with a "YES and NO" MsgBox, we will be using an if statement to check how the user responded to a dialog box. The statement only includes an if scenario, if the condition isn't met, the script simply exits – you can easily extend this if necessary.

$question = MsgBox(4, "Question", "Do you want to restart now?")
If $question == 6 Then
  Shutdown(2)
EndIf

The shutdown option value of "2" represents a reboot, and the variable result of "6" would mean that the "YES" button has been clicked.

More on message boxes

  1. How to use MsgBox in AutoIt

Tell us what you think:

  1. How to declare and work with variables in AutoIt, as well as some background information.
  2. Everything you need to know about working with minimized windows.
  3. How to set the request headers when performing HTTP requests.
  4. This Tutorial will focus on post requests in AutoIt, using the Winhttprequest.5.1 object.
  5. How to use while, for, and do until loops, and how to loop through arrays and object in AutoIt.

More in: AutoIt Tutorials