It handle the password in a secure way.

Look at this as a template and do the necessary changes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$username  = Read-host "Enter username"
$securePwd = Read-host "Enter password" -AsSecureString
$securePwd = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePwd))

$taskSplat = @{
    taskName = 'TaskName'
    action   = New-ScheduledTaskAction –Execute 'runScheduledTask.cmd'
    trigger  = New-ScheduledTaskTrigger -Daily -At 05:00
    user     = $UserName
    password = $securePwd
    settings = New-ScheduledTaskSettingsSet
    taskPath = '\Folder\'
}

Register-ScheduledTask @taskSplat [-force]