6

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?

4
  • 1
    Are you passing from a PS script to a PS Script? Commented Mar 7, 2013 at 2:51
  • 1
    The problem is that the script is expecting an array, but when you run powershell.exe -file ... (etc.) the input string @('a','b','c') is not coerced into an array. If you run the script from a PowerShell prompt, the problem will not occur. Commented Mar 7, 2013 at 3:23
  • 1
    AdqBill - I need the script to be in a schedule task with each task giving a different location. How would you run the scrip then with out using my method? Commented Mar 7, 2013 at 3:44
  • AthomSefre - Nope, just want to send an array to a PS script. Commented Mar 7, 2013 at 3:46

3 Answers 3

8

Use the -command switch instead.

Like this:

powershell -Command "&{C:\ParaTestingArray.ps1 -Location A,B,C}"

From the about_powershell:

For writing a string that runs a Windows PowerShell command, use the format:

     "& {<command>}"

Where the quotation marks indicate a string and the invoke operator (&) causes the command to be executed.

Sign up to request clarification or add additional context in comments.

Comments

1

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

5 Comments

Thanks mjolinor. The way you do it I get the same result as you. What is wrong with the way I am doing it? Can you try it my way and let me know what you get. Thanks agian!!!
I get the same result you do if I call it that way. Not sure how you'd do that.
I think the problem is that there's no way to present an array as a CMD string argument. You'll need to use a delimited string, and split it.
$args means "the arguments passed to the script or function." It can be iterated the same as an array.
This technique does not work when you need to pass in the array using a named parameter, i.e. when both args populate the same parameter. WHat is being shown when iterating over the $args variable is actually iterating over two different arguments. The answer from Fridden is the correct way to pass an array in a sungle argument to a script using powershell.exe.
0

The

Powershell.exe -Command "&{C:\script.ps1 -ParmArray 1,2,3}"

trick worked for me - but it feels unfortunately hacky. Definitely counter-intuitive after the rest of my powershell experience.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.