It’s good to to power off your PC when you’re not using it, but do you ever forget and leave it on? Here’s how to configure Windows to automatically power down at night, but only if you’re not using the PC at the time.

We really recommend putting your PC to sleep rather than shutting it down as a normal course of action. Sleep and hibernation have both come a long way since the early days of Windows and if you avoid using them because you used to have problems, you might be pleasantly surprised with how well they work now. And if you still have trouble with your PC waking up prematurely, we have some advice for keeping that from happening.

Of course, if you’d really like to shut your PC down automatically instead, we understand. And it isn’t hard to do. You just need to set up a scheduled task with rules to prevent the shut down if you’re up late using the PC.

In the Task Scheduler window, in the “Actions” pane, click “Create Task.”

On the “General” tab of the Create Task window, give the new task whatever name you want. We like putting a “z_” before any user-created tasks just so that they’re easier to find later with a quick alphabetical sort. Select both the “Run whether user is logged on or not” option and “Run with highest privileges” options. On the “Configure for” drop-down, select your version of Windows.

Next, switch to the “Triggers” tab. Click “New” to create a new trigger.

In the New Trigger window, make sure the “Begin the task” dropdown menu is set to “On a schedule.” Set up whatever schedule you like. Here, we’re going with every night at midnight. When you’re done setting up your schedule, click “OK.”

Back in the “Create Task” window, switch over to the “Actions” tab and click “New” to create a new action.

In the “New Action” window, set the “Action” drop-down menu to “Start a program.” Type “shutdown” into the “Program/script” box. In the “Add arguments (optional)” box, type “/S” to have have the task start a basic shutdown command—the same as if you’d clicked the Shut Down button yourself. If you want the command to also force any running applications to close without warning users, type “/S /F” into the “Parameters” box instead. When you’re done, click “OK.”

Back in the “Create Task” window, switch over to the “Conditions” tab. Enable the “Start the task only if the computer is idle for” option and set the timing you’d like. Here, we’re setting the task to kick in only if the computer has been idle for 15 minutes when the shut down time arrives. We’re also setting the task to wait up to one hour for that idle time to happen.

You also have two other options to consider here. Enable the “Stop if the computer ceases to be idle” option to stop the task if you start using your PC. And enable the “Restart if the idle state resumes” to have the task begin measuring idle time again when you stop using your PC. We recommend going ahead and enabling both those options.

Next, switch to the “Settings” tab. Here, you can set your task up so that it will run again every so often if it fails to run successfully. This lets the task shut down your PC if you’re up using it for much longer than you expected to be. Enable the “If the task fails, restart every” option and then set your preferences. Here, we’re having the task restart every thirty minutes and we’re letting it try restarting up to three times. When you’re done, click “OK” to create the task.

Note that you will likely be asked to enter your user account password to finish creating the task. And you can now exit Task Scheduler. From this point on, your PC should shut down automatically at whatever time you scheduled—unless you’re using the PC at that time.