Parameters in calls to functions in PowerShell (all versions) are space-separated, not comma-separated. Also, the parentheses are entirely unnecessary 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(
b, $c) {
"a:
b; c: $c"
}
Execute:
foo 1 2 3
Output:
a: 1; b: 2; c: 3
Answer from x0n on Stack OverflowVideos
Parameters in calls to functions in PowerShell (all versions) are space-separated, not comma-separated. Also, the parentheses are entirely unnecessary 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(
b, $c) {
"a:
b; c: $c"
}
Execute:
foo 1 2 3
Output:
a: 1; b: 2; c: 3
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(:b, $c)

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.
If you are on PowerShell V2 you can use the [ValidateSet()] attribute e.g.:
param(
[Parameter()]
[ValidateSet('foo','bar','baz')]
[string[]]
$Item
)
See the help topic by executing:
man about_functions_advanced_parameters
You can use [ValidateSet()] as Keith demonstates, but this doesn't work very well with a large number of possible values (populating the list when you hit Tab becomes very slow). Another method for getting tab completion is to make a custom enum:
https://devblogs.microsoft.com/powershell/v2-custom-enums/
and cast the parameter as that enum type. This does impose some limitations on the character set that's allowed in your strings (basically just letters and number), but it will populate the tab completion list much faster if it's a relatively large list of values.
Try this:
function CheckCert([string[]]$ComputerNames)
{
$deadline = (Get-Date).AddDays($global:threshold) # Set deadline date
foreach ($computer in $ComputerNames)
{
Invoke-Command -ComputerName $Computer { Dir Cert:\LocalMachine\My } |
foreach {
If ($_.NotAfter -le $deadline)
{
$_ | Select Issuer, Subject, NotAfter, @{N="Expires In (Days)";E={($_.NotAfter - (Get-Date)).Days}}
}
}
}
}
Working with PS 4.0 or later, it´s possible to define as CheckCert([array]$ComputerNames) as well.
Check this solution out. This uses the CmdletBinding() attribute, which provides some additional metadata through the use of the $PSCmdlet built-in variable. You can:
- Dynamically retrieve the command's name, using
$PSCmdlet - Get a list of the parameter for the command, using
Get-Command - Examine the value of each parameter, using the
Get-Variablecmdlet
Code:
function test {
[CmdletBinding()]
param (
[string] $Bar = 'test'
, [string] $Baz
, [string] $Asdf
)
# Get the command name
$CommandName = $PSCmdlet.MyInvocation.InvocationName;
# Get the list of parameters for the command
$ParameterList = (Get-Command -Name $CommandName).Parameters;
# Grab each parameter value, using Get-Variable
foreach ($Parameter in $ParameterList) {
Get-Variable -Name $Parameter.Values.Name -ErrorAction SilentlyContinue;
#Get-Variable -Name $ParameterList;
}
}
test -asdf blah;
Output
The output from the command looks like this:
Name Value
---- -----
Bar test
Baz
Asdf blah
To read the value dynamically use the get-variable function / cmdlet
write-host (get-variable "foo")
To print out all of the parameters do the following
foreach ($key in $MyInvocation.BoundParameters.keys)
{
$value = (get-variable $key).Value
write-host "$key -> $value"
}