Yannick Blondeau
Yannick Blondeau

Reputation: 9621

Function parameters order in PowerShell

I have this code in one of my PowerShell scripts:

function callCommandWithArguments([String] $arg1, [String] $arg2)
{
    [string]$pathToCommand = "C:\command.exe";
    [Array]$arguments = "anArg", "-other", "$arg2", "$arg1";
# the real code is
#   & $pathToCommand $arguments;
# but was not working, so I change it to debug
    Write-Host $pathToCommand $arguments;
}

callCommandWithArguments("1", "2");

As the arguments order is changed in the $arguments array, I would expect this output:

C:\command.exe anArg -other  2 1

But instead I receive a strange:

C:\command.exe anArg -other  1 2

Am I missing something obvious?

Upvotes: 0

Views: 992

Answers (1)

CB.
CB.

Reputation: 60918

try call your function like this:

callCommandWithArguments "1" "2"

In powershell you pass arguments to function without () and just separated by space.

In your code you are passsing a single argument array of type object[]

Upvotes: 4

Related Questions