Search code examples
powershellpowershell-4.0

Powershell seems to get the wrong number of elements in a param array


Assume I have (In file test.ps1):

param (
    [string[]] $one
)
Write-Host $one.Count

If I do:

powershell -File test.ps1 -one "hello","cat","Dog"

I get:

1

But I expect:

3

Why?


Solution

  • "-one" is getting passed in as a whole string as the converting happens before calling the method.

    You could alternatively call it like the following

    powershell -Command {.\test.ps1 -one "hello","cat","Dog"}