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
