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
#### When using advanced functions or scripts with CmdletBinding attribute avoid validating parameters in the body of the script when possible and use parameter validation attributes instead.
115
115
116
-
***AllowNull** Validation Attribute
116
+
***AllowNull** Validation Attribute
117
117
118
118
The AllowNull attribute allows the value of a mandatory parameter to be null ($null).
119
119
@@ -123,10 +123,10 @@ function Get-User {
123
123
[AllowNull()]
124
124
[String]
125
125
$ComputerName
126
-
)
126
+
)
127
127
```
128
128
129
-
***AllowEmptyString** Validation Attribute
129
+
***AllowEmptyString** Validation Attribute
130
130
131
131
The AllowEmptyString attribute allows the value of a mandatory parameter to be an empty string ("").
132
132
@@ -136,10 +136,10 @@ function Get-User {
136
136
[AllowEmptyString()]
137
137
[String]
138
138
$ComputerName
139
-
)
139
+
)
140
140
```
141
141
142
-
***AllowEmptyCollection** Validation Attribute
142
+
***AllowEmptyCollection** Validation Attribute
143
143
144
144
The AllowEmptyCollection attribute allows the value of a mandatory parameter to be an empty collection (@()).
145
145
@@ -149,28 +149,28 @@ function Get-User {
149
149
[AllowEmptyCollection()]
150
150
[String[]]
151
151
$ComputerName
152
-
)
152
+
)
153
153
```
154
154
155
-
***ValidateCount** Validation Attribute
155
+
***ValidateCount** Validation Attribute
156
156
157
157
The ValidateCount attribute specifies the minimum and maximum number
158
158
of parameter values that a parameter accepts. Windows PowerShell
159
159
generates an error if the number of parameter values in the command that
160
-
calls the function is outside that range.
160
+
calls the function is outside that range.
161
161
162
162
```PowerShell
163
163
param (
164
164
[Parameter(Mandatory = $true)]
165
165
[ValidateCount(1,5)]
166
166
[String[]]
167
167
$ComputerName
168
-
)
168
+
)
169
169
```
170
170
171
-
***ValidateLength** Validation Attribute
171
+
***ValidateLength** Validation Attribute
172
172
173
-
The ValidateLength attribute specifies the minimum and maximum number
173
+
The ValidateLength attribute specifies the minimum and maximum number
174
174
of characters in a parameter or variable value. Windows PowerShell generates an
175
175
error if the length of a value specified for a parameter or a variable
176
176
is outside of the range.
@@ -181,39 +181,41 @@ function Get-User {
181
181
[ValidateLength(1,10)]
182
182
[String[]]
183
183
$ComputerName
184
-
)
184
+
)
185
185
```
186
186
187
-
***ValidatePattern** Validation Attribute
187
+
***ValidatePattern** Validation Attribute
188
188
189
189
The ValidatePattern attribute specifies a regular expression that
190
190
is compared to the parameter or variable value. Windows PowerShell generates
191
-
an error if the value does not match the regular expression
192
-
pattern.
191
+
an error if the value does not match the regular expression
192
+
pattern.
193
+
193
194
```PowerShell
194
195
param (
195
196
[Parameter(Mandatory = $true)]
196
197
[ValidatePattern("[0-9][0-9][0-9][0-9]")]
197
198
[String[]]
198
199
$ComputerName
199
-
)
200
+
)
200
201
```
201
202
202
-
***ValidateRange** Validation Attribute
203
+
***ValidateRange** Validation Attribute
203
204
204
205
The ValidateRange attribute specifies a numeric range for each
205
206
parameter or variable value. Windows PowerShell generates an error
206
-
if any value is outside that range.
207
+
if any value is outside that range.
208
+
207
209
```PowerShell
208
210
param (
209
211
[Parameter(Mandatory = $true)]
210
212
[ValidateRange(0,10)]
211
213
[Int]
212
214
$Attempts
213
-
)
215
+
)
214
216
```
215
217
216
-
***ValidateScript** Validation Attribute
218
+
***ValidateScript** Validation Attribute
217
219
218
220
The ValidateScript attribute specifies a script that is used
219
221
to validate a parameter or variable value. Windows PowerShell
@@ -230,12 +232,12 @@ function Get-User {
230
232
[ValidateScript({$_ -ge (get-date)})]
231
233
[DateTime]
232
234
$EventDate
233
-
)
235
+
)
234
236
```
235
237
236
-
***ValidateSet** Attribute
238
+
***ValidateSet** Attribute
237
239
238
-
The ValidateSet attribute specifies a set of valid values for a
240
+
The ValidateSet attribute specifies a set of valid values for a
239
241
parameter or variable. Windows PowerShell generates an error if a
240
242
parameter or variable value does not match a value in the set. In
241
243
the following example, the value of the Detail parameter can only
@@ -247,36 +249,38 @@ function Get-User {
247
249
[ValidateSet("Low", "Average", "High")]
248
250
[String[]]
249
251
$Detail
250
-
)
252
+
)
251
253
```
252
254
253
-
***ValidateNotNull** Validation Attribute
255
+
***ValidateNotNull** Validation Attribute
254
256
255
257
The ValidateNotNull attribute specifies that the parameter
256
258
value cannot be null ($null). Windows PowerShell generates an
257
-
error if the parameter value is null.
259
+
error if the parameter value is null.
258
260
259
261
The ValidateNotNull attribute is designed to be used when the
260
262
type of the parameter value is not specified or when the specified
261
263
type will accept a value of Null. (If you specify a type that will
262
264
not accept a null value, such as a string, the null value will be
263
265
rejected without the ValidateNotNull attribute, because it does not
264
-
match the specified type.)
266
+
match the specified type.)
267
+
265
268
```PowerShell
266
269
param (
267
270
[Parameter(Mandatory = $true)]
268
271
[ValidateNotNull()]
269
272
$ID
270
-
)
273
+
)
271
274
```
272
275
273
-
***ValidateNotNullOrEmpty** Validation Attribute
276
+
***ValidateNotNullOrEmpty** Validation Attribute
274
277
275
-
The ValidateNotNullOrEmpty attribute specifies that the parameter
278
+
The ValidateNotNullOrEmpty attribute specifies that the parameter
276
279
value cannot be null ($null) and cannot be an empty string ("").
277
-
Windows PowerShell generates an error if the parameter is used in
280
+
Windows PowerShell generates an error if the parameter is used in
278
281
a function call, but its value is null, an empty string, or an empty
0 commit comments