Windows can start a program or a script by itself at the times you choose: a backup every night, a clean-up every week, a restart on Sunday morning. The tool for this is Task Scheduler. This guide creates a task with commands and with the wizard, runs it once as a test and shows how to check that it worked.

The examples use Windows Server 2025. Run the commands in PowerShell as Administrator: right-click the Start button and choose Terminal (Admin).

ℹ The times are times of the server

A task set to 3:00 AM starts when the clock of the server shows 3:00 AM. A new server may run on UTC, so check the time zone first: Set the Time Zone on Windows Server and Check That the Clock Is Right.


1. A Script to Run

Keep your scripts in one folder, for example C:\Scripts. These two commands create the folder and a small test script that adds one line to a log file each time it runs. Later, put your own commands into the script.

New-Item -ItemType Directory -Path C:\Scripts -Force
Set-Content -Path C:\Scripts\nightly.ps1 -Value 'Add-Content -Path C:\Scripts\nightly.log -Value ("Ran at " + (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " as " + $env:USERNAME)'

2. Create the Task With Commands

$action   = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File C:\Scripts\nightly.ps1"
$trigger  = New-ScheduledTaskTrigger -Daily -At 3am
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable
Register-ScheduledTask -TaskName "Nightly script" -Action $action -Trigger $trigger -Settings $settings -User "SYSTEM" -RunLevel Highest
TaskPath TaskName       State
-------- --------       -----
\        Nightly script Ready
  • The action is what Windows starts: here PowerShell with your script.
  • The trigger is when: here every day at 3:00 AM. Other triggers are -Weekly -DaysOfWeek Sunday -At 4am, -Once -At "12/24/2026 6pm" and -AtStartup for every start of the server.
  • -StartWhenAvailable: if the server was off at that time, the task runs as soon as possible after the next start.
  • -User "SYSTEM": the task runs whether or not somebody is logged in, and no password has to be stored. -RunLevel Highest gives it administrator rights.

3. Run It Once as a Test

Do not wait for the night. Start the task now and look at the result:

Start-ScheduledTask -TaskName "Nightly script"
Get-ScheduledTaskInfo -TaskName "Nightly script" | Format-List LastRunTime, LastTaskResult, NextRunTime
LastRunTime    : 12/2/2026 3:17:55 AM
LastTaskResult : 0
NextRunTime    : 12/3/2026 3:00:00 AM

LastTaskResult 0 means that the program ended without an error. 267011 means that the task has not run yet. Any other number is the error code of the program. The test script also left its line in the log:

Get-Content C:\Scripts\nightly.log
Ran at 2026-12-02 03:17:55 as SERVER01$

The name of the server with $ is how the SYSTEM account appears.

4. The Same in the Task Scheduler Window

Press Windows key + R, type taskschd.msc and press Enter. Open Task Scheduler Library on the left.

Task Scheduler Library with the task Nightly script selected

  • Create Basic Task... (1) starts a wizard.
  • The list (2) shows every task with its Status, Triggers, Next Run Time and Last Run Time. Scroll to the right for Last Run Result.
  • For the selected task (3): Run starts it now, Disable pauses it, Properties changes it and Delete removes it.

The wizard asks four things:

  1. A Name for the task.
  2. The Trigger: Daily, Weekly, Monthly, One time, When the computer starts, When I log on or When a specific event is logged. Then the time.
  3. The Action: choose Start a program.
  4. Program/script, for example powershell.exe, and in Add arguments (optional) the rest, for example -NoProfile -ExecutionPolicy Bypass -File C:\Scripts\nightly.ps1.
⚠ A task from the wizard runs only while you are logged in

After the wizard, select the task and choose Properties. On the General tab choose Run whether user is logged on or not and tick Run with highest privileges. Otherwise the task does not start when nobody is logged in to the server.

5. Example: Restart the Server Every Sunday at 4:00 AM

$action  = New-ScheduledTaskAction -Execute "shutdown.exe" -Argument '/r /t 60 /c "Weekly restart"'
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday -At 4am
Register-ScheduledTask -TaskName "Weekly restart" -Action $action -Trigger $trigger -User "SYSTEM" -RunLevel Highest

Check when it will run next:

Get-ScheduledTaskInfo -TaskName "Weekly restart" | Format-List NextRunTime
NextRunTime : 12/6/2026 4:00:00 AM

The text after /c is written into the log of Windows, so you can see later why the server restarted: Windows Server Restarted? Find Out When and Why.

6. Pause, Change or Remove a Task

Disable-ScheduledTask -TaskName "Nightly script"
Enable-ScheduledTask -TaskName "Nightly script"
Unregister-ScheduledTask -TaskName "Nightly script" -Confirm:$false

The first command pauses the task, the second switches it on again and the third removes it. To change the time, remove the task and create it again with the new trigger, or use Properties in the window.

Good to Know

Was this answer helpful? 0 Users Found This Useful (0 Votes)