How to create a timed script to shut down a Windows computer

Updated: 09/03/2019 by Computer Hope
Computer sleep

On a Windows computer, a sleep timer is available in the Power & Sleep options utility. The sleep timer settings allow you to set a timer to put a computer to sleep after a specified period of inactivity. Unfortunately, if music or a video is playing, the sleep timer will not take effect because there is activity on the computer.

If you want to automatically sleep or shut down your computer after a time, regardless if there is activity on the computer or not, a script is required. Select a link below for the type of script you want to create.

Shut down computer after a pre-defined period of time

A script to shut down a Windows computer after a pre-defined period of time consists of three lines of code. The following example code can be used to automatically shut down your computer.

@echo off
timeout 30
shutdown -s

The first line of code, @echo off, stops the echoing of commands in the Windows command line window. Using @echo off keeps the command line window clean.

The second line of code, timeout 30, is the command telling Windows to wait a period of time before executing the next line of code. The number after timeout is the amount of time, in seconds, that Windows will wait. You can set this value to your desired wait period. By using the timeout command, a countdown is displayed on the command line window, letting you know how much time is left before the computer is shut down. However, if desired, you can press any key on the keyboard to skip the rest of the timer and immediately execute the next line of code.

Timer countdown using timeout command

The third line of code, shutdown -s, is the command to shut down Windows and the computer.

Using Notepad, add these three lines of code to a blank file, then save the file as a .bat batch file.

Save as a batch file in Notepad

Shut down computer after a user-defined period of time

A script to shut down a Windows computer after a user-defined period of time consists of four lines of code. The following example code can be used to automatically shut down your computer-based on a timer defined by the user.

@echo off
set /p timer= "Enter the desired shut down timer in seconds: "
timeout %timer%
shutdown -s

The first line of code, @echo off, stops the echoing of commands in the Windows command line window. Using @echo off keeps the command line window clean.

The second line of code, set /p timer= "... ", is the command that allows the user to define the timer period. The set command initializes the variable, timer in our code, and assigns the value entered by the user to that variable. The /p switch makes the prompt appear, with the text between the quotes after the equal (=) sign. Without the /p switch, the timer variable would be assigned the text after the equal sign, and no user prompt would show.

The third line of code, timeout %timer%, is the command telling Windows to wait a period of time before executing the next line of code. The %timer% after timeout is the variable storing the numeric value, the number of seconds, entered by the user, for which Windows will wait. By using the timeout command, a countdown is displayed on the command line window, letting you know how much time is left before the computer is shut down. However, if desired, you can press any key on the keyboard to skip the rest of the timer and immediately execute the next line of code.

Timer countdown using timeout command with user-defined timer

The fourth line of code, shutdown -s, is the command to shut down Windows and the computer.

Using Notepad, add these four lines of code to a blank file, then save the file as a .bat batch file.

Save script as a batch file in Notepad