You can also use the IsNullOrWhiteSpace method to check if a string variable is not null or empty in PowerShell. This method only works from PowerShell 3.0. It returns True if the variable is null or empty or contains white space characters. If not, it prints False in the output.
You can use the [string]::IsNullOrEmpty($version) method if it is a string.
Conclusion. In JavaScript, the condition “arg == null” can be used to check passed arguments for null values.
Given this basic function:
Function TestFunction {
Param ( [int]$Par1, [string]$Par2, [string]$Par3 )
If ($Par1 -ne $Null) { Write-Output "Par1 = $Par1" }
If ($Par2 -ne $Null -or $Par2 -ne '') { Write-Output "Par2 = $Par2" }
If ($Par3 -ne $Null) { Write-Output "Par3 = $Par3" }
}
TestFunction -Par1 1 -Par3 'par3'
...the output is:
Par1 = 1
Par2 =
Par3 = par3
Even though I didn't pass anything into the $Par2
variable, it still isn't Null or empty. What happened, and how can I rewrite the statement so that the second If-statement evaluates as False and the script-block does not get executed?
(I added the -or $Par2 -ne ''
just to test, it behaves the same with and without it.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With