Search code examples
powershellnull

simple way to check 2 variables at once for null or empty


Seeking a simple way (ideally a single line of code) to check if either of 2 variables is empty something like below, however, this doesn't work...

if ([string]::isnullorempty(var1,var2)) {"empty"} else {"not empty"}

Solution

  • The not operator will convert a string to a boolean. Try this:

    if (!$var1 -or !$var2) {"empty"} else {"not empty"}
    

    I think that !$var1 will return $true if $var1 is null or empty.