Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
November 22, 2022

Leveraging PSObject

In the last article, we dove into the internals of how PowerShell creates and defines objects. It doesn’t matter if it is the result from running Get-Process to a custom object from your function. You should be able to discover the hierarchy of PSBase, PSAdapted, PSExtended, and PSObject. I wrapped up the last article discussing the structure of PSObject.

PS C:\> $p = Get-Process -id $pid
PS C:\> $p.psobject.properties | Group-object MemberType -NoElement

Count Name
----- ----
    7 AliasProperty
    1 CodeProperty
   52 Property
    1 NoteProperty
    8 ScriptProperty

PS C:\> $p.psobject.properties | where Membertype -eq 'aliasproperty' | Select Name,ReferencedMemberName

Name    ReferencedMemberName
----    --------------------
Name    ProcessName
SI      SessionId
Handles Handlecount
VM      VirtualMemorySize64
WS      WorkingSet64
PM      PagedMemorySize64
NPM     NonpagedSystemMemorySize64

This is much easier than trying to parse the output from Get-Member. But how can you take advantage of PSObject? I thought I’d share some code that I use that does exactly that.

Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.