Shedule task with PowerShell

This blog contains a script which can be run to set schedule a task with PowerShell that also runs a PowerShell script. We create 5 tasks using this script when we install SharePoint 2010.

function CreateTask

{
#Change below variables

$userdomain = “Domain name”

$username = “User Account that has sufficient rights”

$TaskName = “My Task”

$ScriptLocation = “C:\scripts\MyScript.ps1″

#You don’t have to change below variables

$TaskPassword = Read-Host “Please enter the password for $userdomain\$username” -AsSecureString

$TaskPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($TaskPassword))

schtasks /create /tn $TaskName /tr “$env:windir\system32\windowspowershell\v1.0\powershell.exe -noninteractive -file $locationBackup” /sc daily /st <span style="color: #ff0000;">02:00</span>/ru $userdomain\$username /rp $TaskPassword /RL Highest| Out-Null

}

CreateTask

Keep in mind that you have to change the time that the task will run on.

Leave a Reply

Your email address will not be published. Required fields are marked *