File not found when attempting to run a scheduled task in windows 10


I am attempting to write a powershell script that schedules another powershell script. Here is the code that I currently have:

    $Action = New-ScheduledTaskAction -Execute 'pwsh.exe' -Argument '-NonInteractive -NoLogo -NoProfile -File "C:\test.ps1"'
    $Trigger = New-ScheduledTaskTrigger -Daily -At 6pm
    $Settings = New-ScheduledTaskSettingsSet
    $Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings
    Register-ScheduledTask -TaskName "Test" -InputObject $Task

This creates the task that I want, but when it is triggered, it fails, and claims that the file was not found. I would also like for it to prompt the user for the administrators password, but I could not figure that out in the documentation. I settled for non interactive, but that isn't really what I want. Thanks in advance for any help.

Edit: the error code is 0x80070002

asked on Super User May 4, 2021 by hufflegamer123 • edited May 6, 2021 by hufflegamer123

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0