title: Cannot create Scheduled task with powershell that has option "run only when user is logged on"
body: <p>Does anyone know how to create a script that will enable me to set the option run only when user is logged on? I have searched for hours but found no good solution. The problem is that this program cannot run in the background and must therefor, from my knowledge run only when user is logged on. Any advice is apreciated</p>
  
  <pre><code>$taskName = 'task name'
  $taskDescription = 'task desc'
  $executable = "program.exe"
  $workingDir = "C:\utv"
  $user = "user"
  $password = "password"
  
  Write-Host "Configuring scheduled task"
  Write-Host " - Executable: $workingDir\$executable"
  Write-Host " - User: $user"
  Write-Host "pw: $password"
  
  $action = New-ScheduledTaskAction -Execute "$workingDir\$executable"
  $trigger = New-ScheduledTaskTrigger -Daily -At 00:01 
  $existingTask = Get-ScheduledTask | Where-Object {$_.TaskName -like     $taskName}
  $principal = New-ScheduledTaskPrincipal -UserId $user
  if ($existingTask) {
      Write-Host "Existing task found, updating..."
      Set-ScheduledTask `
          -TaskName $taskName  `
          -Action $action `
          -Trigger $trigger -User $user -Password $password
  }
  else {
      Write-Host "Creating new task..."
      Register-ScheduledTask `
          -TaskName $taskName `
          -Description $taskDescription `
          -Action $action `
          -Trigger $trigger -User $user -Password $password
  }
  Write-Host "Setting Repetition properties"
  $newTask = Get-ScheduledTask | Where-Object {$_.TaskName -like $taskName}
  
  $newTask.Triggers.repetition.Duration = 'PT23H'
  $newTask.Triggers.repetition.Interval = 'PT60M'
  $newTask.Principal.LogonType = 
  $newTask | Set-ScheduledTask -User $user -Password $password
  
  Write-Host "Done!"
  </code></pre>