Iterate Over PSCustomObject Properties by Using THIS Hidden Property

I was writing some code earlier this week and came across the need to iterate over the properties of a [pscustomobject]. I wanted a function to be able to accept a pscustomobject and use all of the members to form a body on the fly. This command is for an api so I wanted to make it reusable with future versions of the api that may include new parameters.

I started writing a foreach loop and realized that I forgot how to iterate over each property in a pscustomobject. Sounds like the perfect excuse to freshen up on it and document it for the next time I forget :)

Epic picture with PEgasus and some rocky stuff in the sky.

Code Sample

Hidden PSObject Property

PSCustomObjects have a hidden property named psobject. This property contains base object metadata. Let’s look at the hidden properties of a pscustomobject using the -force parameter of Get-Member.

$object = [pscustomobject]@{ Key1 = 'Val1' ; Key2 = 'Val2' }

$object | Get-Member -Force

   TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
----        ----------   ----------
pstypenames CodeProperty System.Collections.ObjectModel.Collection...
psadapted   MemberSet    psadapted {ToString, Equals, GetHashCode, GetType}
psbase      MemberSet    psbase {ToString, Equals, GetHashCode, GetType}
psextended  MemberSet    psextended {Key1, Key2}
psobject    MemberSet    psobject {Members, Properties, Methods, ImmediateB...
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
Key1        NoteProperty string Key1=Val1
Key2        NoteProperty string Key2=Val2

Let’s grab all of the properties by using the .psobject property. We only want to grab noteproperties because there will be other properties that we dont want to iterate over.

$objMembers = $object.psobject.Members | where-object membertype -like 'noteproperty'

#create an empty hashtable to add stuff to.
$form = @{}

foreach ($obj in $objMembers) {
   $form.add( "$($", "$($obj.Value)")

Read More

These are some great posts that go a bit deeper on pscustomobjects and provide lots of relevant info.