arrayspowershellparam

Passing an array to a PowerShell script


I am trying to pass an array to a PowerShell script, but I always get just one value. I have googled my butt off, but I can't find anything. All I need to do is pass an array to the script. Here is my code:

param($Location)
($location).count

Foreach ($loc in $Location)
{

$loc

}

Here is my command I am running:

C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -ExecutionPolicy Unrestricted  -File "C:\ParaTestingArray.ps1" -location Sydney,London

Here is the output:

1
Sydney

For the life of me I can't get it to grab the other value in the array. I have tried using

param([string[]]$Location)

I have tried:

-location "Sydney","London"
-location @(Sydney,London)
-location Sydney London
-location Sydney,London
-location (Sydney,London)

What am I doing wrong?


Solution

  • I cannot reproduce that result:

    $script = @'
    param($Location)
    ($location).count
    
    Foreach ($loc in $Location)
    {
    
    $loc
    
    }
    '@
    
    $script | sc test.ps1
    
    .\test.ps1 sydney,london
    

    2 sydney london

    Edit: This works:

    $args.count
    
    Foreach ($loc in $args)
    {
    
    $loc
    
    }
    

    Called as: powershell.exe -file c:\test.ps1 sydney london