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:


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


If you want to read more about the possible Parameter attribute declarations, you can check out this TechNet article here:

No comments:

Post a Comment