Iterate Over PSCustomObject Properties by Using This Hidden Property

Iterate Over PSCustomObject Properties by Using This Hidden Property

Use this for easy navigation to main pages. This is hidden on your site
Delete me after reading
Post & Video Instructions - This is hidden on your site
  • Latest Posts & Videos, All Posts, and All Videos are served from the CONTENT DATABASE, based on a few properties:
  • Latest Posts & Videos
    1. Type: Post or Video plus Feature As a Feature post or video, it also serves to the home page as a Latest Post or Latest Video. Consider only having 1-3 Feature at a time to avoid cluttering the page.
    2. Status: Live - a post or video marked as Draft will not serve to this page or the home page.
    image
    All Posts or All Videos
    1. Type: Post or Video
    2. Status: Live
  • Make sure to replace the text in the properties: Title, Excerpt, and Published Date so Post display correctly like this:
  • image

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 :)

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.name)", "$($obj.Value)")
}

Read More

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

📄
Content

NameExcerpt
1
2
© 2022 AndrewPla.Tech
3
Take care of yourself because you deserve it