You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Parameter set names in PowerShell are case-sensitive, unlike most other PowerShell elements. This rule ensures consistent casing and proper configuration of parameter sets to avoid runtime errors and improve code clarity.
14
+
Parameter set names in PowerShell are case-sensitive, unlike most other PowerShell elements. This
15
+
rule ensures consistent casing and proper configuration of parameter sets to avoid runtime errors
16
+
and improve code clarity.
15
17
16
18
The rule performs five different checks:
17
19
18
-
1.**Missing DefaultParameterSetName** - Warns when parameter sets are used but no default is specified
19
-
2.**Multiple parameter declarations** - Detects when a parameter is declared multiple times in the same parameter set. This is ultimately a runtime exception - this check helps catch it sooner.
20
-
3.**Case mismatch between DefaultParameterSetName and ParameterSetName** - Ensures consistent casing
21
-
4.**Case mismatch between different ParameterSetName values** - Ensures all references to the same parameter set use identical casing
22
-
5.**Parameter set names containing newlines** - Warns against using newline characters in parameter set names
20
+
1.**Missing DefaultParameterSetName** - Warns when parameter sets are used but no default is
21
+
specified
22
+
1.**Multiple parameter declarations** - Detects when a parameter is declared multiple times in the
23
+
same parameter set. This is ultimately a runtime exception - this check helps catch it sooner.
24
+
1.**Case mismatch between DefaultParameterSetName and ParameterSetName** - Ensures consistent
25
+
casing
26
+
1.**Case mismatch between different ParameterSetName values** - Ensures all references to the same
27
+
parameter set use identical casing
28
+
1.**Parameter set names containing newlines** - Warns against using newline characters in parameter
29
+
set names
23
30
24
31
> [!NOTE]
25
-
> This rule is not enabled by default. The user needs to enable it through settings.
32
+
> This rule isn't enabled by default. The user needs to enable it through settings.
26
33
27
34
## How
28
35
@@ -43,7 +50,7 @@ function Get-Data {
43
50
param(
44
51
[Parameter(ParameterSetName='ByName')]
45
52
[string]$Name,
46
-
53
+
47
54
[Parameter(ParameterSetName='ByID')]
48
55
[int]$ID
49
56
)
@@ -55,7 +62,7 @@ function Get-Data {
55
62
param(
56
63
[Parameter(ParameterSetName='byname')]
57
64
[string]$Name,
58
-
65
+
59
66
[Parameter(ParameterSetName='ByID')]
60
67
[int]$ID
61
68
)
@@ -67,7 +74,7 @@ function Get-Data {
67
74
param(
68
75
[Parameter(ParameterSetName='ByName')]
69
76
[string]$Name,
70
-
77
+
71
78
[Parameter(ParameterSetName='byname')]
72
79
[string]$DisplayName
73
80
)
@@ -100,11 +107,11 @@ function Get-Data {
100
107
param(
101
108
[Parameter(ParameterSetName='ByName', Mandatory)]
102
109
[string]$Name,
103
-
110
+
104
111
[Parameter(ParameterSetName='ByName')]
105
112
[Parameter(ParameterSetName='ByID')]
106
113
[string]$ComputerName,
107
-
114
+
108
115
[Parameter(ParameterSetName='ByID', Mandatory)]
109
116
[int]$ID
110
117
)
@@ -129,7 +136,9 @@ Rules = @{
129
136
130
137
## Notes
131
138
132
-
- Parameter set names are case-sensitive in PowerShell, making this different from most other PowerShell elements
139
+
- Parameter set names are case-sensitive in PowerShell, making this different from most other
140
+
PowerShell elements
133
141
- The first occurrence of a parameter set name in your code is treated as the canonical casing
134
-
- Parameters without [Parameter()] attributes are automatically part of all parameter sets
135
-
- It's a PowerShell best practice to always specify a DefaultParameterSetName when using parameter sets
142
+
- Parameters without `[Parameter()]` attributes are automatically part of all parameter sets
143
+
- It's a PowerShell best practice to always specify a `DefaultParameterSetName` when using parameter
Copy file name to clipboardExpand all lines: docs/Rules/UseConsistentParametersKind.md
+31-20Lines changed: 31 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,35 +1,45 @@
1
+
description: Use the same pattern when defining parameters.
2
+
ms.date: 03/20/2026
3
+
ms.topic: reference
4
+
title: UseConsistentParametersKind
5
+
---
1
6
# UseConsistentParametersKind
2
7
3
8
**Severity Level: Warning**
4
9
5
10
## Description
6
11
7
-
All functions should have same parameters definition kind specified in the rule.
8
-
Possible kinds are:
9
-
1.`Inline`, i.e.:
10
-
```PowerShell
11
-
function f([Parameter()]$FirstParam) {
12
-
return
13
-
}
14
-
```
15
-
2.`ParamBlock`, i.e.:
16
-
```PowerShell
17
-
function f {
18
-
param([Parameter()]$FirstParam)
19
-
return
20
-
}
21
-
```
12
+
All functions should use the same pattern when defining parameters. Possible pattern types are:
13
+
14
+
1.`Inline`
15
+
16
+
```powershell
17
+
function f([Parameter()]$FirstParam) {
18
+
return
19
+
}
20
+
```
21
+
22
+
1.`ParamBlock`
22
23
23
-
* For information: in simple scenarios both function definitions above may be considered as equal. Using this rule as-is is more for consistent code-style than functional, but it can be useful in combination with other rules.
24
+
```powershell
25
+
function f {
26
+
param([Parameter()]$FirstParam)
27
+
return
28
+
}
29
+
```
30
+
31
+
In simple scenarios, both function definitions shown are considered to be equal. The purpose of this
32
+
rule is to enforce consistent code style across the codebase.
24
33
25
34
## How to Fix
26
35
27
36
Rewrite function so it defines parameters as specified in the rule
28
37
29
38
## Example
30
39
31
-
### When the rule sets parameters definition kind to 'Inline':
32
-
```PowerShell
40
+
When the rule sets parameters definition kind to `Inline`:
41
+
42
+
```powershell
33
43
# Correct
34
44
function f([Parameter()]$FirstParam) {
35
45
return
@@ -42,8 +52,9 @@ function g {
42
52
}
43
53
```
44
54
45
-
### When the rule sets parameters definition kind to 'ParamBlock':
46
-
```PowerShell
55
+
When the rule sets parameters definition kind to `ParamBlock`:
0 commit comments