Pass array from cmd.exe batch to powershell script

Consider the following script: array as parameter.ps1

#requires -version 2.0
[CmdletBinding()]
param (
[parameter(Mandatory=$false)] [string[]] $data
)
write-host "items in data array:" $data.count
write-host "first item in data array:" $data[0]

When calling from command line cmd.exe using the following command

powershell -file ".\array as parameter.ps1" -data "data1","data2"

outputs

items in data array: 1
first item in data array: data1,data2

But when calling using the folloing command

powershell -command "&{.\'array as parameter.ps1' -data 'data1','data2'}"

outputs

items in data array: 2
first item in data array: data1

Comments

Popular posts from this blog

SSIS: Set parent package variable from child package

How to decrypt stored password from SSMS registered servers

Insert bulk statement does not support recompile (SQL 2017)