All of these answers do not highlight that when comparing a value with $ null, you must put $ null on the left, otherwise you may run into problems when comparing with the value of the collection type. See: https://github.com/nightroman/PowerShellTraps/blob/master/Basic/Comparison-operators-with-collections/looks-like-object-is-null.ps1
$value = @(1, $null, 2, $null) if ($value -eq $null) { Write-Host "$value is $null" }
The above block is completed (unfortunately). More interestingly, in Powershell, the value of $ can be both $ null and not $ null:
$value = @(1, $null, 2, $null) if (($value -eq $null) -and ($value -ne $null)) { Write-Host "$value is both $null and not $null" }
Therefore, it is important to put $ null on the left so that these comparisons work with collections:
$value = @(1, $null, 2, $null) if (($null -eq $value) -and ($null -ne $value)) { Write-Host "$value is both $null and not $null" }
I think this once again shows the power of Powershell!
Dag Wieers Mar 21 '18 at 16:31 2018-03-21 16:31
source share