liveBook · Manning

PSObject property

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

PSObject.Properties is the PowerShell collection that exposes an object's adapted and extended properties.

PSObject.Properties exposes an object's properties in PowerShell, including the names and values needed to inspect or transform data. Unlike Get-Member, it can inspect an object whose property currently contains no data, and it preserves the display order rather than alphabetizing properties. section 9.1.4 section 9.2.1

Use the collection when an automation must:

  • Discover the arguments exposed by a method when the target property is null. section 9.1.4
  • Count an object's properties before allocating rows or other output structures. section 9.2.1
  • Loop through each property and read its Name and Value. section 9.2.1
  • Keep properties in the order shown by the object instead of Get-Member's alphabetical order. section 9.2.1

Converting operating-system data into a Word table

Collect selected Win32_OperatingSystem properties with Get-CimInstance, assign $Properties = @($object.psobject.Properties), create a two-column table with $Properties.Count rows, and loop through the collection to write each property name and value. The reusable New-WordTableFromObject function then positions the document cursor and adds a paragraph break. section 9.2.1

Listing 9.1 New-WordTableFromObject
Function New-WordTableFromObject {
    [CmdletBinding()]
    [OutputType()]
    param(
        [Parameter(Mandatory = $true)]
        [object]$object
    )
 
    $Properties = @($object.psobject.Properties)          #1
 
    $Table = $Selection.Tables.add(                       #2
    $Word.Selection.Range,
    $Properties.Count,
    2,
[Microsoft.Office.Interop.Word.WdDefaultTableBehavior]::wdWord9TableBehavior
    ,[Microsoft.Office.Interop.Word.WdAutoFitBehavior]::wdAutoFitContent
    )
    for ($r = 0; $r -lt $Properties.Count; $r++) {        #3
        $Table.Cell($r + 1, 1).Range.Text =
        $Properties[$r].Name.ToString()
        $Table.Cell($r + 1, 2).Range.Text =
        $Properties[$r].Value.ToString()
    }
 
    $Word.Selection.Start = $Document.Content.End         #4
    $Selection.TypeParagraph()
}

Browse the complete function showing property collection, table sizing, indexed iteration, cell population, and document continuation. section 9.2.1

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Explains null-safe inspection, ordering, counting, and indexed Name/Value iteration, but does not detail PowerShell object internals.
Examples
Demonstrates method discovery and converts Win32_OperatingSystem output into a two-column Word table.
What it assumes
Assumes familiarity with PowerShell objects, properties, loops, Get-Member, and Word automation; teaches property enumeration from the object itself.
What you can do after
Build reusable PowerShell functions that inspect object properties and write ordered Name/Value data into Word tables.
How it connects
Connects PowerShell objects, Get-Member, WMI, CIM, PowerShell functions, and the Word object model.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.