# Name of Task to create $taskName = "Sync AD to SQL" # Location of .PS1 file $fileLocation = "C:\Scripts\SQL\Sync AD to SQL.ps1" # UserName to run .PS1 file as $user = "domain\userName" # Password for above user $password = "userPassword" # Create Task $argument = "-Noninteractive -Noprofile -Command &'" + $fileLocation + "'" $action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument $argument $trigger = New-ScheduledTaskTrigger -Daily -At 6am $settings = New-ScheduledTaskSettingsSet $inputObject = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings Register-ScheduledTask -TaskName $taskName -InputObject $inputObject -User $user -Password $passwordIf you are not Running the Task as a different user, comment out before the -User as seen below:
Register-ScheduledTask -TaskName $taskName -InputObject $inputObject # -User $user -Password $passwordThere are a bunch of properties that can be tweaked when creating a scheduled task. Below are the links to the appropriate technet articles for each cmdlet used:
New-ScheduledTaskAction
New-ScheduledTaskTrigger
New-ScheduledTaskSettings
New-ScheduledTask
Register-ScheduledTask
Excellent! Thanks for this - I've been looking at this feature for ages. Followed your instructions and it works a treat!
ReplyDeleteThis is great! Thanks! Which version of PowerShell supports the above solution?
ReplyDeleteIt looks like it will run on PowerShell v3, v4, and v5. I have only tested it on v4 though.
Deletehi, how to set up new-scheduledtasktrigger -weekly ???
ReplyDelete-weekly -Day Monday -At 7:00AM << that is what i want to create the schedule...Please help me ^____^
Great readding your blog post
ReplyDelete