Get-CimInstance -ComputerName $pc -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq $compare } | Remove-CimInstance
Because some system profiles have empty values for LocalPath, I get the following:
You cannot call a method on a null-valued expression. At line:52 char:88
Where-Object { $_.LocalPath.split('')[-1] -eq $compare })
Any way either first check for empty value and skip to next?
ty
In PowerShell (Core) 7.1 and above, you can use the null-conditional operator,
?.:As a general caveat:
?.works as expected with properties, with variables it unfortunately and unexpectedly requires the variable name to be enclosed in{...}, because - surprisingly -?is a legal character in a variable name.Set-StrictMode -Version 2; $foo = $null; ${foo}?.ToUpper()works, but using$foo?.ToUpper()instead does not, because PowerShell looks for a variable named$foo?(sic), which doesn't exist. Requesting that this counterintuitive behavior be changed - even though it is technically a breaking change - is the subject of GitHub issue #14025.In Windows PowerShell you can use PowerShell's implicit to-Boolean conversion rules:
Due to use of
$_.LocalPathas an operand with the-andoperator, it is implicitly coerced to a Boolean, and$nullis coerced to$false; similarly, an empty string would yield$false- see the bottom section of this answer for a summary of PowerShell's to-Boolean conversion rules.Since
-andshort-circuits (as does-or), the RHS expression is never evaluated if$_.LocalPathyields$false, thereby avoiding the error.Note that
-eqhas higher precedence than-and(and-or), so no parentheses are needed around the RHS expression - see the conceptual about_Operator_Precedence help topic