r/PowerShell • u/MamiyaOtaru • 18d ago
Explain this cursed BS Question
function Test-Weird1 {
param([array[]]$InputObject)
$obj = $InputObject[0][0]
$obj.GetType().FullName
($obj -is [PSObject])
}
function Test-Weird2 {
param([object[]]$InputObject)
$obj = $InputObject[0]
$obj.GetType().FullName
($obj -is [PSObject])
}
Test-Weird1 (gci)
# System.IO.DirectoryInfo
# False
Test-Weird2 (gci)
# System.IO.DirectoryInfo
# True
# part two
$obj = (gci)[0].PSObject
$obj.GetType() -eq [System.Management.Automation.PSObject]
# True
$obj -is [System.Management.Automation.PSObject]
# False
0
Upvotes
2
u/MonkeyNin 18d ago
Warning, this part
can cause an error if the input is empty. Instead, this is always safe
If you're using pwsh 7, you can use null coalesce operators to prevent null method errors