PowerShell Genie

Create a custom reporting object

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

The [pscustomobject] cast applied to a hashtable literal produces one object whose properties are the hashtable keys, in the order they were written.

The script

1PS> [pscustomobject]
2PS> @{Name=
3PS> "Web01"
4PS> ;Role=
5PS> "IIS"
6PS> ;Healthy=
7PS> $true
8PS> ;Checked=
9PS> Get-Date
10PS> }

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 Name Role Healthy Checked
    2 ---- ---- ------- -------
    3 Analyse IIS True 9/10/2026 12:00:00 PM
    Inspect this example’s structure

    Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

    [pscustomobject] @{Name= "Web01" ;Role= "IIS" ;Healthy= $true ;Checked= Get-Date }
    1. Stage 1[pscustomobject]Positional values or no named parameters

      How it works

      The [pscustomobject] cast applied to a hashtable literal produces one object whose properties are the hashtable keys, in the order they were written. Mixed value types cause no trouble, so a boolean and a DateTime sit alongside strings with no conversion involved.

      Adapting it

      Emit one of these per input item inside a loop and the function returns a collection that pipes cleanly into Sort-Object or Export-Csv with nothing further required.

      If the result is unexpected

      A custom reporting object has inconsistent columns or blank values.

      Different branches can emit different shapes, and missing source values may be silently assigned.

      1. Inspect input values before constructing each PSCustomObject and use consistent property names.

      Validate the result

      Every row has the intended columns and marks unavailable source data explicitly.

      Keep the check reliable

      Define a stable report schema and test empty and partial input cases.

      Primary documentation

      PowerShell objects and pipelines documentation

      Practise the script’s building blocks

      Choose two service properties

      Return only Name and Status for all services.

      Review the lesson

      Continue
      Genie guideCreate a custom reporting object

      The [pscustomobject] cast applied to a hashtable literal produces one object whose properties are the hashtable keys, in the order they were written.