PowerShell DISCUSSION

Why does my PowerShell function get both arguments in the first parameter when I call it like Add(2, 3)?

Started by talegetalulu PowerShell function call syntaxparameter bindingarray argumentnamed parameterssplatting
5 replies 248 views 6 participants
Latest activity · 30 Sep 2026

Why does my PowerShell function get both arguments in the first parameter when I call it like Add(2, 3)?

talegetalulu PowerShell Forum
#1

I defined function Add-Numbers($a, $b) { $a + $b } and called it the way I would in C#: Add-Numbers(2, 3). Instead of 5 it prints 2 and 3 on separate lines. Inside the function $b turns out to be empty and $a holds both numbers.

Why does PowerShell bind the arguments this way, and what is the correct calling syntax, especially when an argument is itself an expression or another function call?

Community replies 5

Re: Why does my PowerShell function get both arguments in the first parameter when I call it like Add(2, 3)?

#2

Functions and cmdlets are called like shell commands: arguments are separated by spaces, with no parentheses. The comma is PowerShell's array operator, so (2, 3) is a single two-element array, and Add-Numbers(2, 3) passes that one array to $a and nothing to $b. Adding $null to an array gives an array again, not a sum, hence the 2 and 3.

The correct call is Add-Numbers 2 3, or with names, Add-Numbers -a 2 -b 3, which is clearer and independent of parameter order.

Re: Why does my PowerShell function get both arguments in the first parameter when I call it like Add(2, 3)?

#3

The confusion comes from .NET methods, which do use parentheses and commas: [Math]::Max(2, 3) and $text.Substring(0, 4) are correct. The rule is: methods on objects and types take a parenthesised, comma-separated list; commands (functions, cmdlets, scripts, executables) take space-separated arguments.

PowerShell can catch the mistake for you. With Set-StrictMode -Version Latest at the top of the script, calling a function with method syntax and more than one argument raises an error saying the function was called as if it were a method.

Re: Why does my PowerShell function get both arguments in the first parameter when I call it like Add(2, 3)?

#4

Declaring types makes the bug visible even without strict mode. With function Add-Numbers { param([int]$a, [int]$b) $a + $b }, the wrong call fails at once because an array cannot be converted to [int], instead of quietly returning something odd. Adding [Parameter(Mandatory)] in front of a parameter also stops a call that leaves it out from running with an empty value: PowerShell prompts for it, or fails in a non-interactive session.

Re: Why does my PowerShell function get both arguments in the first parameter when I call it like Add(2, 3)?

#5

Parentheses are still needed, but for grouping each argument, not for the call. Arguments are parsed in argument mode, where bare text is a string: Add-Numbers 2+3 4 passes the string "2+3". Wrap expressions and nested calls individually: Add-Numbers (2 + 3) 4 returns 9, and Add-Numbers (Get-Offset) 4 passes the result of the inner command.

To pass a real array as one argument, the comma form is exactly right: Show-Items 1, 2, 3 binds a three-element array to the first parameter.

Re: Why does my PowerShell function get both arguments in the first parameter when I call it like Add(2, 3)?

#6

When a call has many parameters, splatting keeps it readable. Put the arguments in a hashtable and pass it with @ instead of $: $p = @{ a = 2; b = 3 } followed by Add-Numbers @p is the same as Add-Numbers -a 2 -b 3. The keys must match parameter names. An array can be splatted the same way for positional arguments. This is also the clean way to build up optional parameters conditionally before a single call.

TEP COMMUNITY