r/PowerShell • u/MamiyaOtaru • Jul 28 '26
Question Explain this cursed BS
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
View all comments
2
u/MonkeyNin Jul 29 '26
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