PowerShell Working with Objects
A PSCustomObject is a bundle of named values that you can read by name. It groups related facts, like a book title and its author, into one item.
Build an object and read its properties
Create a book object with a title, an author, and a page count. Then read two of its properties.
$book = [PSCustomObject]@{ Title = "Dune"; Author = "Frank Herbert"; Pages = 412 }
$book.Title
$book.PagesRead a text property and a number property
Properties can hold text or numbers. Read each one the same way, with a dot and its name.
$song = [PSCustomObject]@{ Title = "Blue"; Artist = "Mia"; Minutes = 3 }
$song.Artist
$song.MinutesPull a property out of the pipeline
Send the object through the pipeline. Select-Object -ExpandProperty then prints just that one value.
$book = [PSCustomObject]@{ Title = "Dune"; Author = "Frank Herbert"; Pages = 412 }
$book | Select-Object -ExpandProperty AuthorExample
$movie = [PSCustomObject]@{ Title = "Arrival"; Year = 2016; Rating = 8 }
$movie.Title
$movie.Year
$movie | Select-Object -ExpandProperty RatingKey points
- Property: Each named value inside an object is called a property.
- Dot reading: You read a property with a dot, like $book.Title.
- Creation: You build one with [PSCustomObject] followed by names and values in braces.
- Pipeline: Objects travel through the pipeline, so Select-Object can pull one property out of them.
- Values only: Print a property, never the whole object, so the output stays as plain values.
In this example, a movie object holds a title, a year, and a rating. The title and year are read with a dot, and the rating is pulled out of the pipeline with Select-Object -ExpandProperty.
Note
Property names are case-insensitive, so $book.title reads the same property as $book.Title.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
How do you read the Title property of an object stored in $book?