PowerShell PSCustomObject
[pscustomobject]@{ } turns a hashtable into a real object with named properties. This is how you produce your own structured output instead of printing text.
Documentation: Microsoft Learn
Overview
It matters because everything downstream in PowerShell expects objects. Once your data is a pscustomobject you can sort it, filter it with Where-Object, select columns, and pipe it straight into Export-Csv or ConvertTo-Json and get correct results. A script that writes formatted strings with Write-Host can do none of that, because its output is only readable by a human looking at the screen.
Unlike a plain hashtable, the cast preserves the order you wrote the keys in, so your columns come out in the order you intended.
The habit to build is to end every function and script by emitting objects rather than text. Let whoever calls it decide whether to format, export or filter. That single change is most of what separates a script that gets reused from one that gets rewritten.
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.
$student = [pscustomobject]@{ Name = 'Ada';Score = 91 }
$student- Stage 1$student
Positional values or no named parameters
An example to inspect
Preview the example. Sign in for the full lesson and activities.
$student = [pscustomobject]@{ Name = 'Ada';Score = 91 }
$studentWhat happens
The hashtable defines two keys, and the [pscustomobject] cast turns it into an object with a Name property and a Score property. Reading $student.Score returns 91 as a number, not as the string 91, which means it will sort and compare numerically.
Worth remembering
- [pscustomobject]@{ } builds a real object from a hashtable.
- Property order is preserved, unlike a plain hashtable.
- Objects can be sorted, filtered and exported. Text cannot.
- Emit objects from your scripts, not formatted strings.
Primary documentation
Try it
Illustrative output · values vary by environment
Example reader
Inspect this page's example
Run supported commands against the practice data, or inspect the example without executing it.
Ready to inspect.
Builds a two-property object and reads one property.
Show reference output
Illustrative output · values vary by environment
Name Score ---- ----- Ada 91