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

Updated: 08/31/2020 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.

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

Shut down the computer after a pre-defined time

A script to shut down a Windows computer after a pre-defined time consists of three lines of code. The following example code can 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 time before executing the next line of code. The number after timeout is the time, in seconds, that Windows waits. You can set this value to your desired wait period. Using the timeout command, a countdown is displayed on the command line window, showing you how much time is left before the computer shuts down. However, pressing any key on the keyboard skips the rest of the timer and immediately executes 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 the computer after a user-defined time

A script to shut down a Windows computer after a user-defined time consists of four lines of code. The following example code can 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 waits. Using the timeout command, a countdown is displayed to let you know how much time is left before shutting down. However, if desired, press any key on the keyboard to skip 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