Skip to content

Commit 1b73296

Browse files
authored
Add information about numeric parameter names (#11623)
1 parent 9889511 commit 1b73296

File tree

9 files changed

+268
-92
lines changed

9 files changed

+268
-92
lines changed
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Introduces advanced functions that are a way to create cmdlets using scripts.
33
Locale: en-US
4-
ms.date: 01/20/2023
4+
ms.date: 01/02/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_functions_advanced?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Functions_Advanced
@@ -14,8 +14,8 @@ Introduces advanced functions that are a way to create cmdlets using scripts.
1414
## Long description
1515

1616
A cmdlet is a single command that participates in the pipeline semantics of
17-
PowerShell. This includes binary cmdlets, advanced script functions, CDXML, and
18-
Workflows.
17+
PowerShell. This includes binary cmdlets, PowerShell advanced script
18+
functions, CDXML, and Workflows.
1919

2020
Advanced functions allow you create cmdlets that are written as a PowerShell
2121
function. Advanced functions make it easier to create cmdlets without having to
@@ -28,6 +28,12 @@ the Cmdlet attribute that's used in compiled cmdlet classes to identify the
2828
class as a cmdlet. For more information about this attribute, see
2929
[about_Functions_CmdletBindingAttribute][03].
3030

31+
The parameters of the function are variables declared in the `param()`
32+
statement. You can use the optional `[Parameter()]` attribute alone or in
33+
combination with the `[Alias()]` attribute or any of the parameter validation
34+
attributes. For more information about how to declare parameters, see
35+
[about_Functions_Advanced_Parameters][02].
36+
3137
The following example shows a function that accepts a name and then prints a
3238
greeting using the supplied name. Also notice that this function defines a name
3339
that includes a verb (Send) and noun (Greeting) pair like the verb-noun pair of
@@ -37,36 +43,29 @@ a compiled cmdlet. However, functions aren't required to have a verb-noun name.
3743
function Send-Greeting
3844
{
3945
[CmdletBinding()]
40-
Param(
46+
param(
4147
[Parameter(Mandatory=$true)]
4248
[string] $Name
4349
)
4450
45-
Process
51+
process
4652
{
4753
Write-Host ("Hello " + $Name + "!")
4854
}
4955
}
5056
```
5157

52-
The parameters of the function are declared using the `Parameter` attribute.
53-
This attribute can be used alone, or it can be combined with the Alias
54-
attribute or with several other parameter validation attributes. For more
55-
information about how to declare parameters (including dynamic parameters that
56-
are added at runtime), see [about_Functions_Advanced_Parameters][02].
57-
58-
The actual work of the previous function is performed in the `process` block,
59-
which is equivalent to the **ProcessingRecord** method that's used by compiled
60-
cmdlets to process the data that's passed to the cmdlet. This block, along with
61-
the `begin` and `end` blocks, is described in the
62-
[about_Functions_Advanced_Methods][01] topic.
58+
This function performs the work in the `process` block, which is equivalent to
59+
the **ProcessingRecord** method used in compiled cmdlets. The `process` block
60+
and the other named blocks are described in
61+
[about_Functions_Advanced_Methods][01].
6362

6463
Advanced functions differ from compiled cmdlets in the following ways:
6564

6665
- Advanced function parameter binding doesn't throw an exception when an array
6766
of strings is bound to a **Boolean** parameter.
68-
- The `ValidateSet` attribute and the `ValidatePattern` attribute can't pass named
69-
parameters.
67+
- The `ValidateSet` attribute and the `ValidatePattern` attribute can't pass
68+
named parameters.
7069
- Advanced functions can't be used in transactions.
7170

7271
## See also
@@ -76,10 +75,12 @@ Advanced functions differ from compiled cmdlets in the following ways:
7675
- [about_Functions_Advanced_Parameters][02]
7776
- [about_Functions_CmdletBindingAttribute][03]
7877
- [about_Functions_OutputTypeAttribute][04]
78+
- [about_Variables][06]
7979

8080
<!-- link references -->
8181
[01]: about_Functions_Advanced_Methods.md
8282
[02]: about_Functions_Advanced_Parameters.md
8383
[03]: about_Functions_CmdletBindingAttribute.md
8484
[04]: about_Functions_OutputTypeAttribute.md
8585
[05]: about_Functions.md
86+
[06]: about_Variables.md

reference/5.1/Microsoft.PowerShell.Core/About/about_Functions_Advanced_Parameters.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Explains how to add parameters to advanced functions.
33
Locale: en-US
4-
ms.date: 07/02/2024
4+
ms.date: 01/02/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Functions_Advanced_Parameters
@@ -28,6 +28,60 @@ the parameters in a command. Splatting is valid on simple and advanced
2828
functions. For more information, see [about_Functions][14] and
2929
[about_Splatting][17].
3030

31+
## Parameter declaration
32+
33+
Parameters are variables declared in the `param()` statement of a function or
34+
script block. You can use the optional `[Parameter()]` attribute alone or in
35+
combination with the `[Alias()]` attribute or any of the parameter validation
36+
attributes.
37+
38+
Parameter names follow the rules for variable names. Parameter names consist of
39+
decimal digits, alphabetic characters, and underscores. For a complete list of
40+
naming rules, see [about_Variables][20].
41+
42+
> [!IMPORTANT]
43+
> It's possible to name a parameter using only decimal digits. Using numeric
44+
> parameter names isn't recommended because it can lead to confusion with
45+
> positional parameters.
46+
47+
Consider the following example:
48+
49+
```powershell
50+
function TestFunction {
51+
param (
52+
[switch] $100,
53+
[string] $200
54+
)
55+
56+
"100: $100"
57+
"200: $200"
58+
}
59+
```
60+
61+
If you try to use the parameters, PowerShell interprets them as negative
62+
numbers passed as positional parameter.
63+
64+
```powershell
65+
PS> TestFunction -100 -200 Hello
66+
100: False
67+
200: -100
68+
$args: -200 Hello
69+
```
70+
71+
The output shows that PowerShell has bound the value `-100` to the `$200`
72+
parameter variable. The remaining positional values are bound to `$args`. To
73+
work around the issue, you can use splatting to pass the parameter values.
74+
75+
```powershell
76+
PS> $ht = @{100 = $true; 200 = 'Hello'}
77+
PS> TestFunction @ht
78+
100: True
79+
200: Hello
80+
$args:
81+
```
82+
83+
For more information, see [about_Splatting][17].
84+
3185
## Type conversion of parameter values
3286

3387
When you supply strings as arguments to parameters that expect a different
@@ -104,6 +158,8 @@ At line:13 char:15
104158
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-Date_Func
105159
```
106160

161+
For more information, see [about_Type_Conversion](about_Type_Conversion.md).
162+
107163
## Static parameters
108164

109165
Static parameters are parameters that are always available in the function.
@@ -1177,7 +1233,7 @@ True
11771233
- [about_Functions_OutputTypeAttribute][13]
11781234

11791235
<!-- link references -->
1180-
[01]: ./about_comment_based_help.md
1236+
[01]: about_comment_based_help.md
11811237
[02]: /dotnet/api/system.management.automation.runtimedefinedparameter
11821238
[05]: about_Automatic_Variables.md
11831239
[06]: about_CommonParameters.md
@@ -1192,3 +1248,4 @@ True
11921248
[17]: about_Splatting.md
11931249
[18]: about_Tab_Expansion.md
11941250
[19]: about_Wildcards.md
1251+
[20]: about_Variables.md

reference/5.1/Microsoft.PowerShell.Core/About/about_Variables.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes how variables store values that can be used in PowerShell.
33
Locale: en-US
4-
ms.date: 03/07/2024
4+
ms.date: 01/02/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_variables?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Variables
@@ -321,15 +321,16 @@ Alphanumeric variable names can contain these characters:
321321
- Underscore (`_`) character.
322322
- Question mark (`?`) character.
323323

324-
The following list contains the Unicode category descriptions. For more
325-
information, see [UnicodeCategory][17].
324+
The following list contains the .NET names of the Unicode categories with a
325+
description. For more information, see [UnicodeCategory][17].
326326

327-
- **Lu** - UppercaseLetter
328-
- **Ll** - LowercaseLetter
329-
- **Lt** - TitlecaseLetter
330-
- **Lm** - ModifierLetter
331-
- **Lo** - OtherLetter
332-
- **Nd** - DecimalDigitNumber
327+
- **Lu** - UppercaseLetter - an uppercase letter
328+
- **Ll** - LowercaseLetter - a lowercase letter
329+
- **Lt** - TitlecaseLetter - a digraph encoded as a single character with the
330+
first part uppercase
331+
- **Lm** - ModifierLetter - a modifier letter
332+
- **Lo** - OtherLetter - other letters, including syllables and ideographs
333+
- **Nd** - DecimalDigitNumber - a decimal digit
333334

334335
To create or display a variable name that includes spaces or special
335336
characters, enclose the variable name with the curly braces (`{}`) characters.
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Introduces advanced functions that are a way to create cmdlets using scripts.
33
Locale: en-US
4-
ms.date: 01/20/2023
4+
ms.date: 01/02/2025
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_functions_advanced?view=powershell-7.4&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Functions_Advanced
@@ -14,8 +14,8 @@ Introduces advanced functions that are a way to create cmdlets using scripts.
1414
## Long description
1515

1616
A cmdlet is a single command that participates in the pipeline semantics of
17-
PowerShell. This includes binary cmdlets, advanced script functions, CDXML, and
18-
Workflows.
17+
PowerShell. This includes binary cmdlets, PowerShell advanced functions, and
18+
CDXML cmdlets.
1919

2020
Advanced functions allow you create cmdlets that are written as a PowerShell
2121
function. Advanced functions make it easier to create cmdlets without having to
@@ -28,6 +28,12 @@ the Cmdlet attribute that's used in compiled cmdlet classes to identify the
2828
class as a cmdlet. For more information about this attribute, see
2929
[about_Functions_CmdletBindingAttribute][03].
3030

31+
The parameters of the function are variables declared in the `param()`
32+
statement. You can use the optional `[Parameter()]` attribute alone or in
33+
combination with the `[Alias()]` attribute or any of the parameter validation
34+
attributes. For more information about how to declare parameters, see
35+
[about_Functions_Advanced_Parameters][02].
36+
3137
The following example shows a function that accepts a name and then prints a
3238
greeting using the supplied name. Also notice that this function defines a name
3339
that includes a verb (Send) and noun (Greeting) pair like the verb-noun pair of
@@ -37,36 +43,29 @@ a compiled cmdlet. However, functions aren't required to have a verb-noun name.
3743
function Send-Greeting
3844
{
3945
[CmdletBinding()]
40-
Param(
46+
param(
4147
[Parameter(Mandatory=$true)]
4248
[string] $Name
4349
)
4450
45-
Process
51+
process
4652
{
4753
Write-Host ("Hello " + $Name + "!")
4854
}
4955
}
5056
```
5157

52-
The parameters of the function are declared using the `Parameter` attribute.
53-
This attribute can be used alone, or it can be combined with the Alias
54-
attribute or with several other parameter validation attributes. For more
55-
information about how to declare parameters (including dynamic parameters that
56-
are added at runtime), see [about_Functions_Advanced_Parameters][02].
57-
58-
The actual work of the previous function is performed in the `process` block,
59-
which is equivalent to the **ProcessingRecord** method that's used by compiled
60-
cmdlets to process the data that's passed to the cmdlet. This block, along with
61-
the `begin` and `end` blocks, is described in the
62-
[about_Functions_Advanced_Methods][01] topic.
58+
This function performs the work in the `process` block, which is equivalent to
59+
the **ProcessingRecord** method used in compiled cmdlets. The `process` block
60+
and the other named blocks are described in
61+
[about_Functions_Advanced_Methods][01].
6362

6463
Advanced functions differ from compiled cmdlets in the following ways:
6564

6665
- Advanced function parameter binding doesn't throw an exception when an array
6766
of strings is bound to a **Boolean** parameter.
68-
- The `ValidateSet` attribute and the `ValidatePattern` attribute can't pass named
69-
parameters.
67+
- The `ValidateSet` attribute and the `ValidatePattern` attribute can't pass
68+
named parameters.
7069
- Advanced functions can't be used in transactions.
7170

7271
## See also
@@ -76,10 +75,12 @@ Advanced functions differ from compiled cmdlets in the following ways:
7675
- [about_Functions_Advanced_Parameters][02]
7776
- [about_Functions_CmdletBindingAttribute][03]
7877
- [about_Functions_OutputTypeAttribute][04]
78+
- [about_Variables][06]
7979

8080
<!-- link references -->
8181
[01]: about_Functions_Advanced_Methods.md
8282
[02]: about_Functions_Advanced_Parameters.md
8383
[03]: about_Functions_CmdletBindingAttribute.md
8484
[04]: about_Functions_OutputTypeAttribute.md
8585
[05]: about_Functions.md
86+
[06]: about_Variables.md

0 commit comments

Comments
 (0)