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 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.

  • Shut down the computer after a pre-defined time.
  • Shut down the computer after a user-defined time.

@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 amount of 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, you can press any key on the keyboard to skip the rest of the timer and immediately execute the next line of code.

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.

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 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.

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.

  • How to shut down or restart the computer with a batch file.
  • Full listing of MS-DOS and Windows command line commands.
  • All questions and answers on MS-DOS and the Windows command line.
  • How to shut down a computer.
  • How to adjust power, standby, and sleep settings.
  • Is it better to leave the computer on or turn it off?
  • Is it ok for a laptop to hibernate or go to Standby mode?
  • Batch file help and support.