22 Votes

Windows Batch Script: Computer Shutdown

Tutorial by Stefan Trost | Last update on 2024-04-24 | Created on 2013-11-03

With this little two-line batch script, you can simply shut down a computer with a Windows operating system:

@echo off
%SYSTEMROOT%\system32\shutdown.exe -s -t 0

To use the script, just create a new blank plain text file, copy the 2 lines into it and change the file extension from "txt" to "bat".

When you then double-click on this file, the computer will be automatically shut down without demand.

Useful Extensions to the Script

Of course, we can already use the script presented exactly as it is written there. Nevertheless, depending on your needs, you can also make some modifications and extensions to the script to get even more out of it. We would like to take a look at what is possible and how it works in these following sections:

Embedding in other Scripts

This script can be used, for example, within large batch script files requiring a long processing time. For example, batch files in which many actions are performed or a big number of programs are started and executed.

If you want the computer to be shut down automatically after the script has been processed, it is sufficient to add the second line from the example code to the end of the long batch script as the last line. This saves the user from having to wait in front of the computer for the script to finish, as the computer will automatically shut down once the work is done, so that you do not have to care about the shutdown anymore for yourself.

Delayed Computer Shutdown

The 0 respectively the last parameter in the command indicates that the shutdown should be performed within 0 seconds - that means, immediately and without any time delay. Using a different number there, makes the computer shut down with a delay, as for example this call shows:

%SYSTEMROOT%\system32\shutdown.exe -s -t 180

Here we pass "-t 180", which means that we switch off the PC after 3 minutes (3 x 60 seconds after calling the command). By the way, the letter "t" stand for "time" here.

The specification of the time must always be given as an integer in seconds. A maximum value of 315360000 (10 years) can be used. If we omit specifying a time (i.e. if we just write "shutdown.exe -s" without any time specification), a t-value of 30 is assumed by default, meaning the computer shuts down after half a minute.

Aside from that, it is also important to note that the time definition is always to be understood in the form of something like a "countdown" and not in the form of an absolute time. This means that if, for example, we give the command that the computer should be shut down in one hour (3600 seconds) at 5:00 p.m., the computer will only actually be shut down at 6:00 p.m. if the computer is kept switched on the whole time remained. However, if the computer goes into standby in the meantime, let's say at 5:10 p.m., the countdown for the shutdown is also stopped and only resumes after the computer has been switched on again. We should keep this in mind, especially when defining longer periods of time.

Restart Computer

The parameter "s", which we have used in all of our previous examples so far, stands for the word "shutdown". Even if the program name "shutdown.exe" does not immediately suggest it, we can also use the same program to restart our computer either immediately or after an arbitrary period of time defined by the "t" parameter. The only thing we have to change in the command that we have learned so far is to replace the parameter "s" with "r". The letter "r" stands for "restart".

%SYSTEMROOT%\system32\shutdown.exe -r -t 0

With this call to "shutdown.exe" we restart our computer immediately after calling the command. Of course, we can also work with a time delay this time, as the next example shows:

%SYSTEMROOT%\system32\shutdown.exe -r -t 60

Again we can define any time between 0 and 315360000 seconds using the (time) parameter "t". In the example, we let the computer restart exactly after 60 seconds respectively after one minute.

Put Computer into Hibernation or Sleep Mode

So far we have looked at the command line parameters that we can use to either shut down or restart our computer. However, beyond that, Windows can also be put either into its hibernation state or in its sleep mode to save energy but also to be able to quickly resume an existing session. These states can also be activated using a script and are useful, for example, if after executing a long script we want to continue at the same point where we left off with all open programs and files.

The parameter to hibernate our computer is "h". Unfortunately, this parameter cannot be used in the same way as we have previously seen for "s" (shutdown) or "r" (restart). Furthermore, there is no shutdown parameter for the sleep mode available at all, so we have to proceed completely differently to activate that state.

Since activating these states is correspondingly more complicated and would take up too much space within this tutorial, I have written a separate tutorial about activating Windows hibernation and sleep mode using a script, in which we can go into more detail than it is possible here in only one paragraph.

Treatment of open Programs and Files

When running a command to shut down our computer automatically, inevitably, the question arises what actually happens if we still have applications or files open at the time the shutdown should take place. The answer to this question depends on the one hand on our time delay specified via the parameter "t", on the other hand we can also consciously control the behavior by using the parameter "f" (which we have not yet introduced). In this section we would like to take a look at how this works in detail.

Basically, if the computer is switched off, two different things can happen: Either all programs are automatically closed without any prompt and unsaved changes to files are lost, or the user is given the opportunity to save unsaved files before shutting down or restarting by displaying a message before shutting down that programs with unsaved changes are still open.

Which of these two cases occurs depends on our call. The following table provides an overview of the different cases that can occur:

CommandAction
shutdown -sImmediate shutdown without asking or waiting
shutdown -s -t 0Inquiry "Open Programs" / "Waiting for Program" / "Unsaved Changes"
shutdown -s -t 60Immediate shutdown without asking or waiting
shutdown -s -f -t 0Immediate shutdown without asking or waiting

If we use shutdown.exe without any other parameter such as f or t, an automatic shutdown occurs without further inquiry. This means that in this case, unsaved files would be lost. The same applies if a time delay of at least one second has been defined via the t parameter (if we call "shutdown -s" without specifying t, a time delay of 30 seconds is used by default so that t is also over 0 in this case). If, on the other hand, we define an immediate shutdown without any time delay using "-t 0", a query about the unsaved files occurs by default, so that saving is still possible at the last moment. If we want to prevent this, we can additionally pass the parameter f (force stop). If this parameter is set, "-t 0" will also cause an immediate shutdown without any inquiry. For t > 0, f is automatically set and cannot be deactivated.

I tested these calls on various computers with Windows 11, Windows 10, Windows 7 and Windows XP. Windows 11, Windows 10 and Windows 7 behaved as described in my tests. Windows XP, on the other hand, only closed programs with unsaved files without further inquiry if "f" was explicitly set. The other systems always implicitly assumed "f" to be set as soon as t was greater than 0. Unfortunately, shutdown.exe does not know any other parameter to override this behavior in order to retain the storage option even in the event of a delayed shutdown or restart.

Although we always used the parameter "-s" (shutdown) for the example calls shown in the table, "-r" (restart) behaves the same way.

Withdraw Command

The (time) parameter "t" can be used to define very long waiting times until the computer is shut down or restarted. As already mentioned above, a time delay of up to ten years is possible. Only after this time has elapsed the initiated shutdown (-s) or restart (-r) will be carried out. But what can we do if, within this possibly year-long period of time, we change our mind after some time has passed and we want to withdraw the command?

The developers of "shutdown.exe" thought of exactly this case and gave us the parameter "a" (abort). The following example shows how we can use this parameter to cancel our scheduled action:

%SYSTEMROOT%\system32\shutdown.exe -a

Accordingly, we simply have to call "shutdown.exe" with the sole parameter "a". This will delete the planned shutdown or restart. The "a" parameter works regardless of which of these two actions we have scheduled.

Of course, this command only works if it is called within the time period defined by "t" and not afterwards. If we have called "shutdown.exe" with "-t 0" (immediate execution), it is always too late to abort. If, on the other hand, we have started the program with "-t 60" (60 seconds), for example, and we issue the command to abort after, for example, 30 seconds, it is not too late and the automatic shutdown or restart can still be prevented.

Change Command

Also if we want to change the time delay of an already scheduled shutdown, we must first call the command "shutdown.exe -a" before we can define a new time via "shutdown.exe".

Let's assume that we have set the shutdown time to 180 seconds from now on and now we would like to retrospectively reduce this time to just 60 seconds. One might now perhaps expect that we could simply overwrite the command by setting another time again. However, if we would use the following sequence of commands, the second command would fail because the shutdown is already set to 180 seconds and the timer is already running. Despite this second call, a shutdown would nevertheless take place only after 180 seconds.

%SYSTEMROOT%\system32\shutdown.exe -s -t 180
%SYSTEMROOT%\system32\shutdown.exe -s -t 60

It would be correct like this:

%SYSTEMROOT%\system32\shutdown.exe -s -t 180
%SYSTEMROOT%\system32\shutdown.exe -a
%SYSTEMROOT%\system32\shutdown.exe -s -t 60

Here we first define a time of 180 seconds for our shutdown again. However, to delete this command, the first thing we need to do is to call "shutdown.exe -a" like we have seen in the last section. Only then we can define a new time for the shutdown (here "shutdown.exe -s -t 60").

Manual Restart

Another way to "withdraw" or "undo" the shutdown command is to manually shut down or restart the computer before the time for the automatic action has been reached. Once we do this, any system shutdowns or reboots scheduled via shutdown.exe will be automatically deleted.

User Notifications

If we use one of the commands presented here to instruct our computer not to shut down or restart immediately, but only after a certain time delay, the countdown that has been started with this, does not just run in the background in secret. Instead, the user is given various messages during the expiring time period, which can differ depending on the length of time delay as well as the Windows version used. In general, these notifications become more intrusive the closer we get to the shutdown time.

  • For short time periods of less than 3 minutes, immediately, a small dialog window appears announcing the shutdown.
  • For time periods between 3 and 10 minutes, two small dialog windows are shown. The first dialog immediately announces the shutdown in X minutes, the second dialog reminds you again 2 minutes before the shutdown.
  • If the time delay exceeds 10 minutes, there is first a notification about the shutdown in the tray, then a further warning 10 minutes before the shutdown and, depending on the system, another dialog 2 minutes before the shutdown.

On some versions of Windows, the last dialog box before shutdown showed a countdown of the remaining time in the tests, on other versions of Windows only the time was displayed without any count down.

List of Parameters

Finally, we would like to summarize all the parameters used in this tutorial as well as some other useful parameters in the following list. By clicking on "Details" you can jump to the relevant section within this tutorial where the parameter in question is described and you can find examples of how to use the parameter.

ParameterMeaning
-s(shutdown)Shuts the computer down.Details
-r(restart)Restarts the computer.Details
-h(hibernation)Puts the computer into hibernation state.Details
-l(logout)Logs out the current user.
-a(abort)Cancels the shutdown or restart.Details
-t x(time)Ensures that the shutdown or restart is carried out only after x seconds. If this parameter is omitted, the action occurs after 30 seconds by default. 0 leads to an immediate execution, a maximum value of 315360000 (10 years) can be used. Cannot be used with -h.Details
-f(force stop)Use this parameter to force potentially active applications to close without warning. Unsaved changes to files can be lost. If t is greater than 0, the -f parameter is automatically applied.Details
-i(interface)Shows a graphical user interface that allows you to configure the shutdown manually.

Shutdown.exe knows a few other parameters in addition to the parameters listed in this compilation, but these are not required for the purposes described here. For example, the parameter "c" (comment) can be used to define a comment regarding the restart or shutdown, or the parameter "d" can be used to specify a reason for the restart or shutdown. If you would like to find out more about these and other parameters, you can simply use the command line call "shutdown -?". With this command, shutdown.exe prints a list of all possible parameters directly to the command line. Please note that the available parameters may differ depending on the Windows version used.

Background Information

Our script calls the program shutdown.exe, which initiates the shutdown respectively the restart process of Windows and which can also be used to put the computer into sleep mode. This program is located in the subdirectory "system32" of the Windows folder (typically this results in the path "C:\Windows\System32").

However, since the system folder can have a different absolute path on each computer (depending on where Windows has been installed), instead of using the mentioned absolute path, we use the constant "%SYSTEMROOT%" instead, which always gives us the correct folder for the respective system installation of the computer on which the command is executed. After all, it would be conceivable that someone has not installed Windows on the C: drive or that the folder differs from an older Windows version to future Windows versions.

Alternative Writings

Usually, Windows finds the program "shutdown.exe" even without specifying a path. This allows us to shorten the command (here for an immediate shutdown) to the following line:

shutdown -s -t 0

Furthermore, the parameters can also be passed with a slash instead of the hyphen used in most of the examples. With this, the command mentioned would look like this:

shutdown /s /t 0

Generally, all of the options mentioned should work, regardless of whether you use the hyphen or the slash and regardless of whether you define the full path or not. If in doubt and to be on the safe side, you should of course try out the command on the computer on which the script is to run before using it productively.

ReplyPositiveNegativeDateVotes
00 Votes

Is there any possibility to call shutdown.exe on a modern Windows in such a way that, despite a defined time delay (i.e. parameter t > 0), everything is not just closed automatically, but it is asked, in case that unsaved files or programs are still open?
2024-04-22 at 13:47

ReplyPositive Negative
11 Vote

Since the parameter t has to be equal to zero to get a demand respectively to prevent the forced closing of programs, you can do the following, for example:

timeout /t 180 /nobreak
shutdown -s -t 0

Here the parameter t remains at 0, so that the message regarding programs that are still open is not suppressed. At the same time, we simply removed the time delay from the shutdown command with a timeout of 3 minutes (180 seconds) and added it in advance.
2024-04-22 at 22:15

Positive Negative
Reply
Reply

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

The Askingbox Search

Info | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.