Passing just an array to ArgumentList in Powershell

What is the expected output from this command?

invoke-command -ScriptBlock { param($list) $list | %{ write-host $_ } } -ArgumentList @(@(1,2,3))

I would have expected:

> 1
> 2
> 3

But it seems to be:

> 1

After some experimenting and trying various ways to get this working as expected, I finally resorted to google and found a post on StackOverflow. So the way to do this is by putting a comma in the array to satisfy the parser:

invoke-command -ScriptBlock { param($list) $list | %{ write-host $_ } } -ArgumentList @(,@(1,2,3))

And now I get the output:

> 1
> 2
> 3

I don't think this is intuitive and I hope this will be addressed in the future. At least for now there is a way to do it.

blog comments powered by Disqus