Scheduling a PowerShell script in the Windows Task Scheduler is not so hard, but when you want to run a script with arguments, it can be a bit tricky. For instance, you could run a script which will send an e-mail, so you would indicate the SMTP server but how can you do it? That’s very simple, let’s see how to perform this task.
Open the Windows Scheduler and create a basic task:
Add a task trigger
I will run this script every day
Select “Start a program”
If you work with Windows PowerShell, you must use the following path:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
If you work with PowerShell Core, you must use the following path:
Now, you must add arguments with the following syntax:
-Command "& 'C:\Script\MyScript.ps1'"
Be careful with the quotes!
Now, I can add arguments with the full syntax:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "& 'C:\Script\MyScript.ps1' -SMTPServer smtp.mail.com"
Thanks for reading! You can follow me on Twitter @PrigentNico