Skip to content

How to Create a Scheduled PowerShell Task on Windows Server Print

  • 0

Applies to: Windows Server 2019, 2022 and 2025.

A scheduled task can run repeatable maintenance without an interactive session. Use a managed service account where supported and log every execution.

Automation magnifies both correct and incorrect actions. Use source control, peer review, test environments and least privilege. Visit managed Windows VPS hosting.

Prerequisites

  • Store the script in a protected path.
  • Define runtime identity and permissions.
  • Make the script idempotent.
  • Choose retry and failure monitoring.

Procedure

Step 1: Create script path

Restrict modification rights.

New-Item -ItemType Directory -Path 'C:\ProgramData\CorpScripts' -Force

Step 2: Create action

Run PowerShell non-interactively.

$action=New-ScheduledTaskAction -Execute 'PowerShell.exe' -Argument '-NoProfile -NonInteractive -File C:\ProgramData\CorpScripts\Maintenance.ps1'

Step 3: Create trigger

Schedule daily example.

$trigger=New-ScheduledTaskTrigger -Daily -At 2:00AM

Step 4: Create settings

Stop hung tasks and allow restart.

$settings=New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Hours 2) -RestartCount 2 -RestartInterval (New-TimeSpan -Minutes 10)

Step 5: Register

Use approved service identity and avoid passwords in scripts.

Register-ScheduledTask -TaskName 'Corp Maintenance' -Action $action -Trigger $trigger -Settings $settings -User 'CORP\svc-maintenance'

Step 6: Test

Start manually and inspect result.

Start-ScheduledTask -TaskName 'Corp Maintenance'

Verification

Check state, last run and result code.

Get-ScheduledTask -TaskName 'Corp Maintenance'
Get-ScheduledTaskInfo -TaskName 'Corp Maintenance'

Rollback

Disable-ScheduledTask for immediate rollback, then Unregister-ScheduledTask after preserving logs.

Security notes

Task registration that prompts for a password should be performed interactively and replaced with gMSA where feasible.

Official references

For a hosted environment with setup and onboarding, explore our managed Windows VPS plans.


Was this answer helpful?

« Back