Schedule PowerShell Scripts that Require Input Values


Summary: Learn how to use the Windows Task Scheduler to run Windows PowerShell scripts that require input values.


Hey, Scripting Guy! QuestionHey, Scripting Guy! I have a Windows PowerShell script that collects information from a computer and writes the results to a text file. I would like to be able to schedule the running of this Windows PowerShell script. The problem is that the Windows PowerShell script requires an input parameter (the path to the text file) and I do not want to re-write the script. In fact, I have another Windows PowerShell script that I will use to create the Scheduled Tasks on a large number of computers in our domain, and the path to the text file will vary depending on the IP subnet upon which the computer resides.

— PS


Hey, Scripting Guy! AnswerHello PS, Microsoft Scripting Guy Ed Wilson here. Well, I was up early this morning and decided to have a coffee day today. The snow we had yesterday received a glazing over of rain mixed with ice during the evening, and this morning the front yard is glistening as it reflects the light from the street. It is still cold (20 degrees Fahrenheit -6 degrees Celsius according to my conversion module seen in the following figure).


Actually, the reason I am up early this morning is not to look at the icy snow, nor to drink a cup of clandestine coffee. The reason I am up is to work on the 2011 Scripting Games. The 2011 Scripting Games will be similar to the 2010 Scripting Games and therefore, for scripters that want to get a jump on the competition, they should review the 2010 Scripting Games documentation. The Study Guide would be an excellent place to start, as would be the Scripting Wife posts. There will be some changes (improvements hopefully) and that is what I was contemplating with the early morning ice and caffeine.

Anyway, I decided to check the Scripter@Microsoft.Com email, and PS, I ran across your message. Of course, it is possible to pass parameters to a Windows PowerShell script that is run with a Scheduled Task. The BiosScriptWithParameter.ps1 script is an example of a Windows PowerShell script that obtains information about the bios of a local computer and writes that information to an ASCII text file. It accepts one mandatory parameter which is the path to the text file to hold the output. Because I did not use the append switch for the Out-File Windows PowerShell cmdlet, the script will overwrite the text file if it already exists, or create the file if it does not exist. The Out-File cmdlet is not smart enough to create a folder in the path if it does not exist. Therefore, the script will generate an error if the folder does not exist. This error is shown in the following figure.


The complete BiosScriptWithParameter.ps1 script appears here.



       HelpMessage=”Path to file”)]


Get-WmiObject -Class win32_bios |

Out-File -FilePath $path -Encoding ascii


To create a Scheduled Task that runs the BiosScriptWithParameter.ps1, I begin by selecting the Basic Task Wizard. The program to run is PowerShell, and the argument to the program looks like the following code.

-command “& ‘c:\fso\BiosScriptWithParameter.ps1’ ‘c:\fso\myBiosTextFile.txt'”


Instead of what I did yesterday (when I added everything on a single line and let the Task Scheduler sort it out) today I specifically break the commands down.

I am passing a value for the command parameter of PowerShell.exe (the executable for the Windows PowerShell console). I then use the ampersand inside a pair of quotation marks. The ampersand as used in this line of code is called the invocation operator and it tells Windows PowerShell to interpret the string that follows it as an executable command. The command therefore accepts the first string as the path to the script, and the second string as value for the input parameter (the path) for the script. The thing that makes the command easier to read is the use of the double quotation marks on the outside of the command, and the use of the single quotation marks inside the command. In this manner, the two strings (script and file path) can easily be distinguished. The Scheduled Task action appears in the following figure.


If I have to schedule a task to run a script that uses two input parameters, I add the second parameter after the first parameter. I must use single quotation marks to enclose the string, and place it prior to the closing double quotation marks. An example script that accepts two input parameters is seen here.



       HelpMessage=”Path to file”)]



       HelpMessage=”WMI Class to Query”)]


Get-WmiObject -Class $class |

Out-File -FilePath $path -Encoding ascii


This time when I create the scheduled task, I will add the value for my second parameter as it appears in this line of code.

-command “& ‘c:\fso\ScriptThatAcceptsTwoInputParameters.ps1’ ‘c:\fso\myBiosTextFile.txt’ ‘Win32_bios'”


As I work through the wizard, I use the Start a Program option of the wizard, and supply the above command as the argument as seen in the following figure.


If the script required three arguments, the process would continue in a similar manner.

PS, that is all there is to using the Task Scheduler to run Windows PowerShell scripts that require input parameters. Input week will continue tomorrow when I will talk about how to add values to objects through the Windows PowerShell pipeline.

I invite you to follow me on Twitter or Facebook. If you have any questions, send email to me at or post them on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.


Ed Wilson, Microsoft Scripting Guy

Comments (4)

  1. Jeff25 says:

    After hours of scratching my head trying to get Task Scheduler to execute my Powershell script that contained parameters, I came across this example and it worked the first time!  Thanks Ed!

  2. MsTech says:

    Hi Scripting Guy,

    I am running a powershell script which pull free disk space from all the serves in the n/w. the script looks for servers_c.txt (which has the list of servers saved in same folder as script) and sends HTML report via email.

    When I run it from powershell it works fine but when I use it in task scheduler >> it send email but with no information.

    How can I have this script use servers_c.txt and send email.

    Please advise !!

  3. Deon says:

    Hi Scripting Guy,
    Thanks for a great blog! It has helped me A LOT!!
    I am struggling to get a (basic) script to execute from the cmd line and then as a scheduled task. It doesn’t throw any errors but does not execute, as I have no output. Executing it in the shell works fine. I suspect the problem is it defines a function which
    I need to load first. (This has to happen at runtime for the session only, so I cannot put it in profile). I would like the script to remain simple (i.e. no modules etc.)

    The script is:
    Function Get-BTSEvents
    BEGIN {}
    $dt = Get-Date
    $rd = $dt.AddDays($Days)

    Get-Eventlog -Logname $LogName |
    Where-Object {$_.Source -match $Source -and ($_.entryType -Match "Error" -or $_.entryType -Match "Warning") -and $_.TimeGenerated -gt $rd} |
    Format-Table TimeWritten, Source, EventID, Message -auto -HideTableHeaders |
    Out-File -filepath "C:MyFoldersTechnologyPowershellScriptsTestEventLogMon.txt" -Width 255 -append
    END {}
    The command is:
    powershell -noexit -command "& { . ‘K:My DocumentsProjectBizTalkToolsScriptsEventLogMonitorParamEventLogMon_v2.ps1’; ‘Get-BTSEvents -LogName Application -Source VSS -Days -1’}

    Powershell fires up when running the command, but doesn’t execute the function. Could it be the ";" between the commands?

    Any suggestions (from anyone) will be greatly appreciated.
    Kind Regards

  4. bill says:

    Hi Scripting Guy,

    What about a script with optional parameters? How do I tell the script which parameter I am trying to pass the value for?