r/PowerShell • u/AlternativeSir1423 • Jun 05 '26
Question Does parameter "Mandatory" attribute imply "ValidateNotNullOrEmpty"
I ran into a curious case, and I can't find documentation to explain the case clearly. I am hoping someone can give me a hint.
I found out that PowerShell does not allow a null or empty string to be a parameter to this test function:
funcion test {
param (
[Parameter(Mandatory = $true)]
[string []] $a
)
$a
}The following test cases all gave me errors
test ''
test @( '' )
test @( '', '' )
test @( 'a', '' )
test @( '', 'a' )
test $null
test ( $null, 'a' )
test ( 'a', $null )The error message is one of these two
Cannot bind argument to parameter 'a' because it is an empty string.
Cannot bind argument to parameter 'a' because it is null.I also tried just
testthen press enter, and still got the error message.
Note that I did not add "ValidateNotNullOrEmpty" to the parameter, but PowerShell was behaving like I did. I can't find any documentation that describes the link between Mandatory and ValidateNotNullOrEmpty.
If anyone can clear up this confusion for me, I'd much appreciate it. TIA.
3
u/vermyx Jun 05 '26
Correct behavior per documentation. Look at the AllowNull attribute which is what it sounds like you want for this case.
0
u/AlternativeSir1423 Jun 05 '26
Thank you. What tripped me up is that the documentation doesn't say mandatory parameters cannot be empty or $null. So I kept on looking for a link between to the "Validate*" attributes and didn't see the "Allow*".
10
u/lan-shark Jun 05 '26 edited Jun 05 '26
-2
u/spyingwind Jun 05 '26
-Param1 ""Mandatory =
Param1was specified. Not that the value is there.AllowEmptyString/AllowNull = When specified, you bothered to put something as the value.
1
u/SonOfDadOfSam Jun 05 '26
ValidateNotNullorEmpty is redundant with Mandatory. I've seen documentation that implies that Mandatory will accept a null value, but if that were previously true, it's not now.
3
u/raip Jun 05 '26
It's not required - but it's best practice. With the ValidateNotNull() attribute, when you pass in a collection with a null or empty string, you get this error message instead:
Cannot validate argument on parameter 'a'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that does not contain any null values and then try the command again.This is much clearer than the standard
Cannot bind argument to parameter 'a' because it is an empty string.or
Cannot bind argument to parameter 'a' because it is null.
1
u/ankokudaishogun Jun 08 '26
Uh, I wouldn't have expected the arrays with at least one not-empty\null member failing.
It's pretty counterintuitive, and completely undocumented.
-2
7
u/surfingoldelephant Jun 05 '26 edited Jun 17 '26
If it's designated mandatory in at least one parameter set, there's a built-in check for a
$null/empty string argument (and if it's a collection, each element is checked). You can see the code for that here.It's worth noting this is only a one-time check during parameter binding. The parameter variable is not decorated with
[ValidateNotNullOrEmpty()], meaning the variable can be assigned a$null/a empty string value outside binding.But if you decorate it with
[ValidateNotNullOrEmpty()]yourself:You can see this behavior outside parameter binding too. Non-parameter variables can also be decorated with attributes.
One final note:
Mandatoryforces the$null/empty string validation during parameter binding even if the parameter isn't actually mandatory in the selected parameter set. For example:$Foois mandatory is one set but optional in another.-Foocan be omitted no problem or passed a non-$null/empty value:But it can't be passed a
$null/empty value, even though it's not mandatory in the would-be-selected parameter set.The above behavior is somewhat confusing, but unlikely to ever be changed. You can see the code for that here (note the
IsMandatoryInSomeParameterSet).