Powershell: Passing an array to a script at command line

Hi,

starting point is a simple powershell shell script testarray.ps1:

[CmdletBinding()]
Param(
  [Parameter(Mandatory=$false,Position=1)][alias("a")][string[]]$ArrayParameter=@()
)
write-host "Count:" $ArrayParameter.Count

Calling from a powershell window it is straightforward:

PS D:\> .\testarray.ps1 -a Parameter1,Parameter2
<pre>Count:2</pre>

But if it is called from a cmd shell it is always interpreted as a single string

D:\> powershell.exe -file D:\testarray.ps1 -a Parameter1,Parameter2
Count:1

To get this working use the -Command Parameter instead of the -File parameter and set the Argument between quotation marks

D:\> powershell.exe -Command D:\testarray.ps1 -a "Parameter1,Parameter2"
Count:2

Michael

Advertisment to support michlstechblog.info

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.