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
6
u/Icolan Jul 28 '26
How about formatting it as code?
Or explaining what you think is weird about it.
3
u/BlackV Jul 28 '26
p.s. formatting (you've used inline code not code block)
it'll format it properly OR
Inline code block using backticks
`Single code line`inside normal textSee here for more detail
Thanks
3
u/surfingoldelephant Jul 31 '26 edited Aug 02 '26
In most cases, casting/binding unwraps a
PSObjectwrapper. That explains the first part.[array[]] $InputObjectis a jagged array, so each individual object from the accumulated(gci)input is cast as an array itself. That casting causesPSObjectunwrapping, hence-is [psobject]is$false.Whereas
[Object[]] $InputObjectis a single-dimensional array, which is the same array type as the input. Since the array elements themselves don't need to be cast, the wrappers remain intact.Simpler example:
Unwrapping used to cause ETS members to be lost in PS v2 which was a pretty big issue back then. It was fixed in v3 by having the engine track member info in something known as the resurrection table, so ETS members can persist after an object is unwrapped. And when an object needs to be rewrapped, it can be looked up in the table and have prior info restored.
There's a few exceptions, like strings, which aren't included in the resurrection table. That's why if you ever cast an ETS-decorated string those added members are lost in most cases.
Anyway, hopefully that explains the first part.
As for the second part,
.psobject,.psadapted, etc are actually instances ofPSMemberSet/PSInternalMemberSet, which is basically a special view of an object's members.MshMemberInfois a good starting point in the source if you're interested.Member sets have an associated
PSObjectowner which wraps the base object. As far as I understand, when you call a method on a member set itself, there's an adapter in the engine that forwards the method call to the associatedPSObject(where the actual methods exist).PSObjectof course has aGetType(), hence that's what gets returned as the type.2
u/whatsgoodbaby Jul 28 '26
?
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
1
u/thehuntzman Jul 28 '26
Just at quick-glance it only appears cursed because you are misunderstanding how powershell handles objects. Array[] is an array of System.Array objects. Object[] is an array of objects of indiscriminate type. The second one I'd have to test to accurately explain (the -is PSObject one).
1
u/PinchesTheCrab Jul 29 '26
You've got so much extra stuff here that makes this harder to read. Is this not doing the same thing?
1
u/StartAutomating Jul 30 '26
Ironically enough, I just wrote an explanation of what this is probably looking for.
Basically these functions probably exist to try to debug "weird" array truthy conditions.
0
u/y_Sensei Jul 28 '26 edited Jul 28 '26
Get-ChildItem without any parameters returns an array of object arrays, each of which represents a FileSystemInfo object, which can either be of type DirectoryInfo or of type FileInfo.
The reason for this is that Get-ChildItem is designed to work with different so-called providers behind the scenes (PSProvider), that's why you can use it not just with file sytems, but with any supported provider like for example the Windows registry provider.
This design causes the objects returned by the cmdlet to be enhanced with additional properties defined by their provider, on top of what the "bare" underlying .NET object would look like.
The .NET object is, however, returned too - as the only member of the object array which constitutes each object returned by the cmdlet.
And that's exactly why the $obj variable in your two functions stores different object types - $InputObject[0][0] returns the underlying "bare" .NET object, $InputObject[0] the enhanced provider-based object.
You can verify this by debugging your code in VS Code (look at the objects in the 'Variables' section of the debugger when each function is called).
0
u/BlackV Jul 28 '26
cause its an array ?