Monday, November 2, 2015

Passing null or empty strings as parameters to PowerShell

 

If you have a parameter that is mandatory in your PowerShell script, but you need to also be able to pass either null or empty strings to it, then you need PowerShell to recognize the null or empty string values that you pass!

Fortunately, PowerShell has a mechanism to do just this!

If you want to be able to accept null values for a PowerShell parameter, you can write something like the following:

[Parameter(Mandatory=$True)]
[AllowNull()]
$MyParameter


On the other hand, if you need to be able to accept empty strings, you can do something such as this instead:



[Parameter(Mandatory=$True)]
[AllowEmptyString()]
[string]$MyParameter

If you want to read more about the possible Parameter attribute declarations, you can check out this TechNet article here: https://technet.microsoft.com/en-us/library/hh847743%28v=wps.630%29.aspx

No comments:

Post a Comment