Hi RJames2010,
This is the expected behaviour , due to the where method and cmdlet always returns a collection, which can be empty if there are no matches. That you can do if check if the collection is empty:
$numbers = 1..5
$noMatch = $numbers.Where({$_ -eq 6})
($noMatch | Get-Member).TypeName
# Count because is a collection
if ($noMatch.Count -eq 0) {
Write-Host "The collection is empty."
} else {
Write-Host "The collection is not empty."
}
# Test with $null It won't work
if ($null -eq $noMatch) {
Write-Host "The collection is empty."
} else {
Write-Host "The collection is not empty."
}
As example If $noMatch is empty prints “The collection is empty.” However this is always not null.
References:
- https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/where-object?view=powershell-7.4
- https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_methods?view=powershell-7.3
Let me know if this solve your doubt.
Luis