PowerShell PSObjects

PSCustomObject creation, property manipulation, and calculated properties for structured output.

PSObjects Quick Reference

Inspect any object
Get-Process | Get-Member                     # Properties and methods
Get-Process | Get-Member -MemberType Property
(Get-Service)[0].GetType().FullName          # System.ServiceProcess.ServiceController
Custom objects
[PSCustomObject]@{
    Hostname = "ise-01"
    IP       = "10.50.1.20"
    Role     = "ISE PAN"
    Status   = "Active"
}
Calculated properties
Get-Process | Select-Object Name,
    @{N='MB';E={[math]::Round($_.WorkingSet/1MB)}},
    @{N='CPU_Sec';E={[math]::Round($_.CPU, 2)}}
Build collection of custom objects
$inventory = @()
foreach ($server in $servers) {
    $inventory += [PSCustomObject]@{
        Name   = $server
        Online = Test-Connection $server -Count 1 -Quiet
        OS     = (Get-WmiObject Win32_OperatingSystem -ComputerName $server -ErrorAction SilentlyContinue).Caption
    }
}
$inventory | Format-Table -AutoSize
Add property to existing object
$user = Get-ADUser -Identity AdminErosado -Properties *
$user | Add-Member -NotePropertyName "AuditDate" -NotePropertyValue (Get-Date)

See Also

  • Pipelines — objects as pipeline currency

  • Basics — Select-Object and Format-* cmdlets