Write-Output result identifies as `[pscustomobject]` (`-is [pscustomobject]`)
See original GitHub issuePrerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
(Write-Output 1) -is [pscustomobject] # unexpectedly True, although .GetType() returns Int32
(1) -is [pscustomobject] # False, as expected
$x = Write-Output 1
$x -is [pscustomobject] # also unexpectedly True
$y = 1
$y -is [pscustomobject] # False, as expected
The issue seems reproducible in both PowerShell 5.1
and PowerShell Core 7.2.0-rc.1
.
Expected behavior
False each time (`Int32` is not a `pscustomobject`).
Actual behavior
PowerShell seems to think any object returned by `Write-Output` is somehow also `pscustomobject`.
Result of this issue is we can have a pair of “almost exactly the same” objects, but one of them thinks it’s also a pscustomobject
:
$x = 1
$y = echo $x # I'd assume $y is the exact same thing as $x here...
$x -is [pscustomobject]
$y -is [pscustomobject] # ...but it's not, because these two checks return different things.
Error details
No response
Environment data
Name Value
---- -----
PSVersion 7.2.0-rc.1
PSEdition Core
GitCommitId 7.2.0-rc.1
OS Microsoft Windows 10.0.19043
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:9
Top Results From Across the Web
Write-Output result identifies as `[pscustomobject]` (`
Write-Output result identifies as `[pscustomobject]` (`-is [pscustomobject]`) · Summary · Jobs. Rebase · Run details. Usage · Workflow file.
Read more >Everything you wanted to know about PSCustomObject
PSCustomObject is a simple way to create structured data.
Read more >powershell where-object changes type of psobject to ...
As you can see, the type get's changed. I need to keep the Object as it is (before). How to workaround that? Thanks...
Read more >Chapter 21. Creating objects for output - PowerShell in Depth
The object type— PSObject —is one provided by PowerShell specifically for this purpose. ... If you pipe $obj to Get-Member, look at the...
Read more >BAMCIS.Common.psm1 1.0.0.1
Converts a PSCustomObject to a Hashtable. ... The cmdlet takes a PSCustomObject and converts all of its property key/values to a Hashtable. You...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think there is a need for some documentation on to explain that type accelerators (a) exist and (b) don’t do what one might expect : for example
This makes sense only when one knows that
int
serves as an alias for[int32]
So knowing 1gb is an
int32
but 2gb is anint64
, and-is [int]
tests for-is [System.Int32]
makes sense of the the example.System.
is not needed needed on a type name - there is an implicitusing namespace system
so we can use-is [int32]
and in lot of cases, see the table below, there’s an argument for usingInt32
instead ofint
,Boolean
instead ofbool
etc. (moving a function to a cmdlet in C# doesn’t need to change type names).with
pscustomobject
we getSo
-is [PSCustomObject]
does-is [System.Management.Automation.PSObject]
- I think this is a unique case where isSystem.Management.Automation.X
type and[X]
is a type accelerator which points to a different type givingPSCustomObject
its split personality: interchangeable withpsobject
in some operations, but not, for example, in a castIMHO the original design wasn’t ideal and the behaviour it gives falls in a awkward triple
And the best “fix” for that is to document it. For reference any such help probably needs to explain the following:
This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.