How do I pass multiple parameters into a function in PowerShell?

748,766

Solution 1

Parameters in calls to functions in PowerShell (all versions) are space-separated, not comma separated. Also, the parentheses are entirely unneccessary and will cause a parse error in PowerShell 2.0 (or later) if Set-StrictMode -Version 2 or higher is active. Parenthesised arguments are used in .NET methods only.

function foo($a, $b, $c) {
   "a: $a; b: $b; c: $c"
}

ps> foo 1 2 3
a: 1; b: 2; c: 3

Solution 2

The correct answer has already been provided, but this issue seems prevalent enough to warrant some additional details for those wanting to understand the subtleties.

I would have added this just as a comment, but I wanted to include an illustration--I tore this off my quick reference chart on PowerShell functions. This assumes function f's signature is f($a, $b, $c):

Syntax pitfalls of a function call

Thus, one can call a function with space-separated positional parameters or order-independent named parameters. The other pitfalls reveal that you need to be cognizant of commas, parentheses, and white space.

For further reading, see my article Down the Rabbit Hole: A Study in PowerShell Pipelines, Functions, and Parameters. The article contains a link to the quick reference/wall chart as well.

Solution 3

There are some good answers here, but I wanted to point out a couple of other things. Function parameters are actually a place where PowerShell shines. For example, you can have either named or positional parameters in advanced functions like so:

function Get-Something
{
    Param
    (
         [Parameter(Mandatory=$true, Position=0)]
         [string] $Name,
         [Parameter(Mandatory=$true, Position=1)]
         [int] $Id
    )
}

Then you could either call it by specifying the parameter name, or you could just use positional parameters, since you explicitly defined them. So either of these would work:

Get-Something -Id 34 -Name "Blah"
Get-Something "Blah" 34

The first example works even though Name is provided second, because we explicitly used the parameter name. The second example works based on position though, so Name would need to be first. When possible, I always try to define positions so both options are available.

PowerShell also has the ability to define parameter sets. It uses this in place of method overloading, and again is quite useful:

function Get-Something
{
    [CmdletBinding(DefaultParameterSetName='Name')]
    Param
    (
         [Parameter(Mandatory=$true, Position=0, ParameterSetName='Name')]
         [string] $Name,
         [Parameter(Mandatory=$true, Position=0, ParameterSetName='Id')]
         [int] $Id
    )
}

Now the function will either take a name, or an id, but not both. You can use them positionally, or by name. Since they are a different type, PowerShell will figure it out. So all of these would work:

Get-Something "some name"
Get-Something 23
Get-Something -Name "some name"
Get-Something -Id 23

You can also assign additional parameters to the various parameter sets. (That was a pretty basic example obviously.) Inside of the function, you can determine which parameter set was used with the $PsCmdlet.ParameterSetName property. For example:

if($PsCmdlet.ParameterSetName -eq "Name")
{
    Write-Host "Doing something with name here"
}

Then, on a related side note, there is also parameter validation in PowerShell. This is one of my favorite PowerShell features, and it makes the code inside your functions very clean. There are numerous validations you can use. A couple of examples are:

function Get-Something
{
    Param
    (
         [Parameter(Mandatory=$true, Position=0)]
         [ValidatePattern('^Some.*')]
         [string] $Name,
         [Parameter(Mandatory=$true, Position=1)]
         [ValidateRange(10,100)]
         [int] $Id
    )
}

In the first example, ValidatePattern accepts a regular expression that assures the supplied parameter matches what you're expecting. If it doesn't, an intuitive exception is thrown, telling you exactly what is wrong. So in that example, 'Something' would work fine, but 'Summer' wouldn't pass validation.

ValidateRange ensures that the parameter value is in between the range you expect for an integer. So 10 or 99 would work, but 101 would throw an exception.

Another useful one is ValidateSet, which allows you to explicitly define an array of acceptable values. If something else is entered, an exception will be thrown. There are others as well, but probably the most useful one is ValidateScript. This takes a script block that must evaluate to $true, so the sky is the limit. For example:

function Get-Something
{
    Param
    (
         [Parameter(Mandatory=$true, Position=0)]
         [ValidateScript({ Test-Path $_ -PathType 'Leaf' })]
         [ValidateScript({ (Get-Item $_ | select -Expand Extension) -eq ".csv" })]
         [string] $Path
    )
}

In this example, we are assured not only that $Path exists, but that it is a file, (as opposed to a directory) and has a .csv extension. ($_ refers to the parameter, when inside your scriptblock.) You can also pass in much larger, multi-line script blocks if that level is required, or use multiple scriptblocks like I did here. It's extremely useful and makes for nice clean functions and intuitive exceptions.

Solution 4

You call PowerShell functions without the parentheses and without using the comma as a separator. Try using:

test "ABC" "DEF"

In PowerShell the comma (,) is an array operator, e.g.

$a = "one", "two", "three"

It sets $a to an array with three values.

Solution 5

Function Test([string]$arg1, [string]$arg2)
{
    Write-Host "`$arg1 value: $arg1"
    Write-Host "`$arg2 value: $arg2"
}

Test "ABC" "DEF"
Share:
748,766
Nasir
Author by

Nasir

Updated on September 14, 2021

Comments

  • Nasir
    Nasir over 2 years

    If I have a function which accepts more than one string parameter, the first parameter seems to get all the data assigned to it, and remaining parameters are passed in as empty.

    A quick test script:

    Function Test([string]$arg1, [string]$arg2)
    {
        Write-Host "`$arg1 value: $arg1"
        Write-Host "`$arg2 value: $arg2"
    }
    
    Test("ABC", "DEF")
    

    The output generated is

    $arg1 value: ABC DEF
    $arg2 value: 
    

    The correct output should be:

    $arg1 value: ABC
    $arg2 value: DEF
    

    This seems to be consistent between v1 and v2 on multiple machines, so obviously, I'm doing something wrong. Can anyone point out exactly what?

  • Nasir
    Nasir about 13 years
    Thanks for the answer. However, I was having issues when calling the function. Didn't matter if the function was declared with param or without it.
  • n0rd
    n0rd over 10 years
    Parens don't separate parameters. They define array.
  • Nasir
    Nasir over 10 years
    Thank you my friend, however, you're a couple of years late :-) The top three answers here had sufficiently addressed the issue. May I suggest heading over to Unanswered section and trying some of those questions?
  • Ashley
    Ashley over 10 years
    Most important thing that has finally helped 'stick' this in my mind is the last sentence: "Parenthesised arguments are used in .NET Methods only."
  • sam yi
    sam yi about 10 years
    I prefer using the paranthesis and comma separated.. is it possible to do this in powershell?
  • x0n
    x0n about 10 years
    @samyi No. Passing a (1,2,3) to a function is effectively treated as an array; a single argument. If you want to use OO method style arguments, use modules: $m = new-module -ascustomobject { function Add($x,$y) { $x + $y } }; $m.Add(1,1)
  • Nasir
    Nasir almost 8 years
    That would be defining parameters for a function, the original question was about how to specify parameters when you call the function.
  • Mister_Tom
    Mister_Tom over 6 years
    +1 for demonstrating My_Function -NamedParamater "ParamValue" function call style. This is a pattern that more PS script code should follow for readability.
  • Keith Langmead
    Keith Langmead over 5 years
    FYI, approved verbs list link no longer works, but can now be found here - docs.microsoft.com/en-us/powershell/developer/cmdlet/…
  • Martin Brandl
    Martin Brandl over 5 years
    @KeithLangmead Thank you Keith, I updated my answer as well.
  • VertigoRay
    VertigoRay about 5 years
    Parens don't define an array, they define a group, which powershell can interpret as an array. Arrays are defined with an at sign (@) before the leading paren, like this empty array: @(); or this array with two numbers: @(1, 2).
  • codewario
    codewario almost 5 years
    Powershell is a shell language and it's common for shell languages to use spaces as a token separator. I wouldn't say Powershell is being different here, it's right in line with other system default shells like cmd, sh, bash, etc.
  • Peter Mortensen
    Peter Mortensen over 4 years
    Java, C++, Ruby, and Python are not from this century (only C#), presuming the Gregorian calendar (though some have evolved more than others).
  • Peter Mortensen
    Peter Mortensen over 4 years
    "Verb-Noun" as in both verb and noun capitalised? Perhaps change the answer to be more explicit about it?
  • Ryan Shillington
    Ryan Shillington over 4 years
    Heh. @PeterMortensen your argument is that I should say "Pick a language from either this century or the last one"? :-)
  • codewario
    codewario over 4 years
    I've never understood the "you should use approved verbs" argument. I'm not contesting the Verb-Noun nomenclature, but what (possibly obscure as I've never run into a problem) issues would I run into with the example function name Setup-Node vs. Initialize-Node or Bootstrap-Node?
  • Martin Brandl
    Martin Brandl over 4 years
    well, consider you expose a Get-Node cmdlet. It would be clear for us that we have to invoke Get-Node, not Retrieve-Node, nor Receive-Node, nor .....
  • Mike Q
    Mike Q almost 4 years
    This did not work for me but I did something like this instead $Results = GetServerInformation $Servers $ServerNames
  • codewario
    codewario almost 4 years
    Splatting is the syntax used to take a hashtable or array and unroll those as named arguments or positional arguments to another command, as opposed to passing the arguments directly to the command as you have done. If you post a question about your splat not working someone may be able to shed some light on your particular issue.
  • IT M
    IT M over 3 years
    Also meaningfull to add the [Alias('something')] before the Param() section. This allows the use of non-verb-approved functions (like eg. gci, ls, dir, cd ...). Example: function Test-Script { [CmdletBinding()] [Alias('tst')] Param() Write-Output "This function works."}
  • Shayan Zafar
    Shayan Zafar about 3 years
    this seems to be different for object method calls such as $var.method($var1,$var2). i get a syntax error if i remove the comma. what's going on here?
  • x0n
    x0n about 3 years
    @ShayanZafar as I said in my original post, that syntax is for .NET framework calls. Only Native powershell functions/cmdlets use spaces.
  • Jason Lee
    Jason Lee almost 3 years
    Is it a typo to have two Position=0 ?
  • user2233949
    user2233949 almost 3 years
    No it's not a typo. That would be the case when you're using parameter sets, which are basically just method overloads. So in that case either name OR id could be passed, but not both. Since both are position 0, PowerShell will figure out which one you're using based on the type, if you don't specify the parameter name. (One is int and one is string)