PoshTip #59 – How to Schedule a PowerShell Script with Arguments?

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

About Nicolas 282 Articles
I work as an IT Production Manager, based in Paris (France) with a primary focus on Microsoft technologies. I have 10 years experience in administering Windows Servers. . I am a Microsoft MVP for Cloud & Datacenter Management. I also received the PowerShell Hero 2016 award by PowerShell.0rg. And finally, I am "MCSE: Cloud Platform and Infrastructure", "MCSA: Windows Servers", "Administering & Deploying SCCM", and CheckPoint CCSA certified.