Passing an array parameter to PS script from Scheduled task

JRV 546 Reputation points

I need to create a scheduled task that runs a PowerShell script. One of its parameters can be an array. For example--

C:\Scripts\Verb-Noun.ps1 -Pattern 'abc def*', 'ghi jkl*', '*mno pqr*'  

I've tried--

POWERSHELL.EXE -ExecutionPolicy Bypass -File "C:\Scripts\Verb-Noun.ps1" -Pattern "'abc def*', 'ghi jkl*', '*mno pqr*'"  
POWERSHELL.EXE -ExecutionPolicy Bypass -File "C:\Scripts\Verb-Noun.ps1" -Pattern 'abc def*', 'ghi jkl*', '*mno pqr*'  
POWERSHELL.EXE -ExecutionPolicy Bypass -File "C:\Scripts\Verb-Noun.ps1" -Pattern "abc def*, ghi jkl*, *mno pqr*"  

(Other variations I can't recall, too!) I thought the first would be correct, but it didn't work, nor did the others.

What is the correct syntax?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,273 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 43,931 Reputation points

    Unfortunately, you cannot. From: about_powershell_exe


    The File parameter cannot support scripts using a parameter that expects an array of argument values. This, unfortunately, is a limitation of how a native command gets argument > values. When you call a native executable (such as powershell or pwsh), it does not know what to do with an array, so it's passed as a string.

    However, you can do something like this:

    POWERSHELL.EXE -ExecutionPolicy Bypass -File C:\Scripts\Verb-Noun.ps1 -Pattern "'abc def*','ghi jkl','mno pqr*'"  

    . . . with the script handling to string-to-array conversion:

    param (  
    $pat = $pattern -split ','  

    Which will work IF there are no commas in the patterns. If there is the possibility of commas in the patterns, then a bit of creative work with a regex should be able to get you around that.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. JRV 546 Reputation points

    Rich, thanks!

    It looks like the easier way for me to accomplish this is with the -Command switch, as whatever is inside the quotes will be treated as though it was typed on a PS command line. Thus--

    POWERSHELL.EXE -Command "& { "C:\Scripts\Verb-Noun.ps1" -Pattern "'abc def*','ghi jkl*','*mno pqr*'" }"  

    The -File syntax is cleaner than the -Command syntax and (arguably) easier to type. So I prefer it where I can use it. But the -Command syntax worked where the -File syntax didn't. So it looks like in this case, -Command is the big winner.

    Commas aren't too likely in the strings (they're portions of printer names), though I'd hate to have that come up someday if a future admin decides commas are good things to have in printer names. So I'd want to add the RegEx. Using the script without adding more code (and especially wrestling with RegEx's!) is a plus.

    And as a bonus, I learned something, which is also a win! Thanks for the help!

    0 comments No comments