Powershell Script Will not Run upon Open or Pass Parameter from Task Scheduler
Windows 10 64-bit. PowerShell 5.1
How to pass arguments from a Windows scheduled task to a PowerShell script.
Add to powershell script %USERPROFILE%\Desktop\1.ps1:
Write-host "
All the arguments are: $args"
Write-host " The first argument is: " $args[0]
Write-host " The second argument is: " $args[1]
cmd /c pause
exit
Build your scheduled task:
A stopped task will sometimes leave behind an instance of conhost.exe. If it is not killed the task might fail. Make the following command the first action of your task:
taskkill /f /im conhost.exe
If there are no spaces and / or quotes in your argument / s you do not have to quote the argument / s.
powershell %USERPROFILE%\Desktop\1.ps1 Argument1 Argument2
If there are spaces and / or quotes in your argument / s you have to single quote the argument / s.
powershell %USERPROFILE%\Desktop\1.ps1 'Argument One' 'Argument Two'
powershell %USERPROFILE%\Desktop\1.ps1 '"ArgumentOne"' '"ArgumentTwo"'
Copy and paste the entire command into a new action in the "Program/script:" box and click OK. Windows 10 Task Scheduler will ask you if you want to split the paste between "Program/Script" and "Add arguments". Click Yes.
See stackoverflow
See Bing search - pass arguments to powershell script
See Bing search - windows scheduled task pass arguments to powershell script