When did Select-Object creates ne PSCustom Objects

This topic contains 9 replies, has 4 voices, and was last updated by Profile photo of Peter Kriegel Peter Kriegel 4 years, 1 month ago.

  • Author
    Posts
  • #4717
    Profile photo of Peter Kriegel
    Peter Kriegel
    Participant

    Howdy!

    I thougt every time I strip down some Properties from an object, Select-Object will create a new PSCustom Object!
    But this lines ar not new PSCustom Objects!
    Get-Item C:\Windows\explorer.exe | Select-Object Fullname,length
    Get-Item C:\Windows\explorer.exe | Select-Object Fullname,@{Name="Size";Expression={$_.length / 1mb}}

    Is there any rule when Select-Object is creating new PSCustom Objects from an given Object?

  • #4732
    Profile photo of Art Beane
    Art Beane
    Member

     Save the Get-Item call into a variable and you will see that it really is a PSCustomObject.

    $File = Get-Item C:\Windows\explorer.exe | Select-Object Fullname,@{Name=”Size”;Expression={$_.length / 1mb}} 
    $File.GetType()
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     False    PSCustomObject                           System.Object
  • #4736
    Profile photo of Peter Kriegel
    Peter Kriegel
    Participant

    Hi Art !

    Thank you for pointing me to that!
    Even i have missed that Get-Member is reporting  Selected.System.IO.FileInfo
    But...

    Is there a rule or explanation when Select-Object is creating new Objects and when not ?

  • #4753
    Profile photo of Art Beane
    Art Beane
    Member

    According to Get-Help Select-Object -Full, the cmdlet output is always:

    Outputs

    The output type is the type of the objects that the cmdlet emits.

    • System.Management.Automation.PSObject

     

  • #4772
    Profile photo of Peter Kriegel
    Peter Kriegel
    Participant

    The documentation is not correct !
    If you use the -First -last and -Unique Parmeters the Objects are left original.
    I think the rule is:

    Whenever you strip down Properties an PScustom Object is generated!?

  • #4774
    Profile photo of Art Beane
    Art Beane
    Member

    OK. It looks like you get a PSCustomObject if you specify a Property name (without -ExpandProperty, of course); otherwise it looks like the orginal object is passed through.

    Examples:

    (Get-QADGroup -SizeLimit 2 | Select -First 1).GetType() returns ArsGroupObject

    but

    (Get-QADGroup -SizeLimit 2 | Select DN -First 1).GetType returns PSCustomObject

  • #4812
    Profile photo of Don Jones
    Don Jones
    Keymaster

    v2 produced a PSCustomObject. v3 produces a Selected.x.x.x object. That's in part so that the formatting system can still differentiate and provide some default formatting views, and so that the ETS can provide some type extensions. It's technically still a PSCustomObject underneath.

    But, overall, what you're dealing with is a concept call immutability. For example, a System.Diagnostics.Process ALWAYS has certain properties. Take one away, and it's no longer that object, by definition. It's like if you have a car, and I chopped off the front part. It's no longer a car, it's a wreck. So when you select anything other than -Property *, what Select-Object produces isn't the original object anymore, and so you see the TypeName change.

  • #4814
    Profile photo of Rob Campbell
    Rob Campbell
    Participant

    So when you select anything other than -Property *, what Select-Object produces isn’t the original object anymore, and so you see the TypeName change.

    Actually -Property * will get you a custom object too. It looks like the original object, but all the methods are gone.

  • #4815
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Yeah, true dat. Anything other than the original ain't the original anymore ;). Like Levi's.

  • #4860
    Profile photo of Peter Kriegel
    Peter Kriegel
    Participant

    So I like to say a big THANKS to you all for clarification. (Especially Don)
    Very hard stuff!

You must be logged in to reply to this topic.