Skip to main content

How to Read a Property of an Object in PowerShell

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

An object keeps its values in properties, and you read one by typing a dot and the property's name, as in $pet.name. This guide reads the properties of an object made from JSON, a property inside a property, and the same property of every item in a list, with real output from PowerShell 7.6.10.

The about_Properties page: "The most common way to get the values of the properties of an object is to use the member access operator (.). Type a reference to the object, such as a variable that contains the object, or a command that gets the object. Then, type the operator (.) followed by the property name."

Syntax​

Syntax
$<variable>.<PropertyName>
$<variable>.<PropertyName>.<PropertyName>
(<command>).<PropertyName>
$<variable>.'<property name with spaces>'
PartWhat it means
$<variable>The object, kept in a variable
.The member access operator: what follows is a member of the object
<PropertyName>The property to read
.<PropertyName>.<PropertyName>A property of a property: each dot reads from the value to its left
(<command>)A command in parentheses, which runs first, so you can read a property of its result
'<property name with spaces>'A property name that has a space goes in quotes

The Example​

The example reads the properties of a pet. The pet starts as JSON text, which ConvertFrom-Json turns into an object. The ConvertFrom-Json page: "The ConvertFrom-Json cmdlet converts a JavaScript Object Notation (JSON) formatted string to a custom PSObject or Hashtable object that has a property for each field in the JSON string." The commands run in the PowerShell 7.6.10 console.

Prerequisites​

  • PowerShell 7. This guide uses PowerShell 7.6.10: $PSVersionTable.PSVersion.ToString() printed 7.6.10.

Step 1: Read a Property with a Dot​

PowerShell
$pet = '{ "name": "Rex", "age": 3, "owner": { "name": "Sam" } }' | ConvertFrom-Json
$pet
$pet.name
$pet.age

macOS-style code window titled PowerShell where JSON text for a pet named Rex, aged 3, whose owner is Sam, is sent to ConvertFrom-Json and saved in $pet, followed by $pet, $pet.name and $pet.age.

Output
name age owner
---- --- -----
Rex 3 @{name=Sam}

Rex
3

The first line sends the JSON text through the pipe, |, to ConvertFrom-Json, and saves the object it makes in $pet. The object has a property for each name in the JSON: name, age and owner, which $pet printed as columns. $pet.name returned the value of the name property, Rex, and $pet.age returned 3.

Step 2: Read a Property of a Property​

The owner property holds another object, with a name property of its own:

PowerShell
$pet.owner
$pet.owner.name
Output
name
----
Sam

Sam

macOS-style terminal window titled PowerShell where $pet is made from the JSON text, $pet.owner prints a table with one name column holding Sam, and $pet.owner.name prints Sam.

$pet.owner returned the owner object, printed as a table. $pet.owner.name read owner first and then name from it, so it returned the text Sam. Each dot reads from the value to its left, so you can go as deep as the object goes.

Step 3: List an Object's Properties​

When you don't know which properties an object has, send it to Get-Member:

PowerShell
$pet | Get-Member -MemberType NoteProperty
Output
TypeName: System.Management.Automation.PSCustomObject

Name MemberType Definition
---- ---------- ----------
age NoteProperty long age=3
name NoteProperty string name=Rex
owner NoteProperty System.Management.Automation.PSCustomObject owner=@{name=Sam}

The Get-Member page: "The Get-Member cmdlet gets the members, the properties and methods, of objects." And: "To get only certain types of members, such as NoteProperties, use the MemberType parameter." The properties that ConvertFrom-Json made are NoteProperty members. The Definition column shows each one's type and value: age is a long, a whole number, set to 3.

Step 4: Read a Property of a Command's Result​

You can skip the variable: put the command in parentheses, then the dot and the property's name.

PowerShell
('{ "name": "Rex" }' | ConvertFrom-Json).name
Output
Rex

The about_Properties page explains the parentheses: "The command is enclosed in parentheses to make sure that it's executed before any properties are accessed."

Step 5: Read the Same Property of Every Item​

The square brackets in this JSON hold a list of two pets:

PowerShell
$pets = '[{ "name": "Rex" }, { "name": "Tom" }]' | ConvertFrom-Json
$pets.Count
$pets.name
Output
2
Rex
Tom

$pets holds two objects, so Count returned 2. The list itself has no name property, so PowerShell read name from each pet and returned both. The about_Properties page calls this member-access enumeration: "Starting in PowerShell 3.0, when you use the member-access operator (.) to access a property that doesn't exist, PowerShell automatically enumerates the items in the collection and returns the value of the property for each item."

Common Mistakes​

MistakeWhat happensDo this instead
A misspelled name: $pet.nmaeNothing, and no error: $null -eq $pet.nmae returned TrueCheck the name with Get-Member, as in Step 3
A command's property without parentheses: '{ "name": "Rex" }' | ConvertFrom-Json.nameThe term 'ConvertFrom-Json.name' is not recognized as a name of a cmdlet, function, script file, or executable program.Put the command in parentheses before the dot, as in Step 4
A name with a space and no quotes: $tag.pet name, where $tag came from '{ "pet name": "Rex" }'ParserError: Unexpected token 'name' in expression or statement.Quote the name: $tag.'pet name' returned Rex

Troubleshooting​

SymptomLikely causeFix
A property prints nothingThe name is misspelled, or the object has no such propertyList the properties with $pet | Get-Member
PropertyNotFoundException: The property 'nmae' cannot be found on this object. Verify that the property exists.Strict mode is on, and the property doesn't existFix the property's name

FAQ​

Why does a wrong property name print nothing?​

Because strict mode is off by default. The Set-StrictMode page lists what happens then, including: "References to non-existent properties return $null". After Set-StrictMode -Version 2.0, $pet.nmae failed with the PropertyNotFoundException in the Troubleshooting table.

Do property names care about upper and lower case?​

No: $pet.NAME returned Rex. The about_Case-Sensitivity page: "PowerShell is guaranteed to be case-insensitive on all systems for the following areas:", and its list includes "Non-dictionary member-access".

Does this work on what Invoke-RestMethod returns?​

Yes. The ConvertFrom-Json page: "You can also use the Invoke-RestMethod cmdlet, which automatically converts JSON content to objects." You read the properties of that object with a dot, the same way.

Conclusion​

$pet.name returned Rex: the object, a dot and the property's name. Add a dot for each level, as $pet.owner.name did, put a command in parentheses before the dot, and quote a property name that has a space.

Additional Resources​