1

I am trying to find out if a variable is null or is an array containing null variables.

So for example, a short expression that will match 'false' for each of the following, but 'true' for anything else:

$nullvariable = $null
$nullvariable1 = @($null)
$nullvariable2 = @($null, $null)
$nullvariable3 = @("1", $null)

# Update - Also need the following to match 'false' as it also breaks Compare-Object
$nullvariable4 = @()

Context: I have some Compare-Object calls where I am trying to avoid 'Cannot bind argument to parameter 'ReferenceObject' because it is null' errors. The first answer here: https://stackoverflow.com/a/45634900/12872270 works but it's not very legible/comprehensible code.

The remaining answers use an 'if' statement which is more legible, but in my testing Compare-Object doesn't just fail for $null, but also any array containing null entries - the given examples using 'if' don't account for that.

Detecting an array containing null variables seems like a separate problem in its own right anyway, hence the question.

3
  • 1
    "I am trying to find out if a variable is null or is an array containing null variables." $null -in $variable would work for all those examples in your question Commented Mar 3, 2023 at 15:22
  • 1
    @SantiagoSquarzon Fab! Never thought of using that. Make it as an answer and I'll happily accept :) Commented Mar 3, 2023 at 15:25
  • 1
    @SantiagoSquarzon And to be honest I'm not sure I've seen that syntax recommended anywhere, not even in the usually fantastic learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/… Commented Mar 3, 2023 at 15:26

2 Answers 2

2

Since containment operators can evaluate against scalars as well as collections, in this case you can either use -contains or -in with any of your variables and the result would be $true:

$null -in $nullvariable         # True
$nullvariable1 -contains $null  # True
$null -in $nullvariable2        # True
$nullvariable3 -contains $null  # True
Sign up to request clarification or add additional context in comments.

1 Comment

OK, found another scenario where Compare-Object fails - $nullvariable4 = @() This fails with this check. Any other ideas?
0

Improving on Santiago Squarzon's helpful answer, which is correct for all the instances I originally supplied, but Compare-Object can still fail when $nullvariable is empty:

$nullvariable = $null
$nullvariable1 = @($null)
$nullvariable2 = @($null, $null)
$nullvariable3 = @("1", $null)
$nullvariable4 = @()

Adding another check as follows seems to do the job:

(!$nullvariable -or $null -in $nullvariable)    # True
(!$nullvariable1 -or $null -in $nullvariable1)  # True
(!$nullvariable2 -or $null -in $nullvariable2)  # True
(!$nullvariable3 -or $null -in $nullvariable3)  # True
(!$nullvariable4 -or $null -in $nullvariable4)  # True

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.