Running PowerShell Script as a Scheduled Task

If you need to run a PowerShell script in regular intervals, why not run it as a scheduled task? Here are some lines that help you create a new scheduled task to run a PowerShell script at 6AM:

#requires -Modules ScheduledTasks
#requires -Version 3.0
#requires -RunAsAdministrator

$TaskName = 'RunPSScriptAt6'
$User= "train\tweltner"
$scriptPath = "\\Server01\Scripts\find-newaduser.ps1"

$Trigger= New-ScheduledTaskTrigger -At 6:00am -Daily 
$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-executionpolicy bypass -noprofile -file $scriptPath" 
Register-ScheduledTask -TaskName $TaskName -Trigger $Trigger -User $User -Action $Action -RunLevel Highest -Force

Twitter This Tip! ReTweet this Tip!