PowerShell: How to Reference Property that Contains a Space


Often you may want to reference a property of an object in PowerShell that contains a space in the property name.

In order to do so, you must wrap the property name in quotes:

$team_data."Team Name"

This particular example selects the property named “Team Name” from the object named team_data.

The following example shows how to use this syntax in practice.

Example: How to Reference Property that Contains a Space in PowerShell

Suppose we create the following object in PowerShell named team_data that contains information about various basketball players:

$my_data = [ordered]@{
  'Team Name' = @('Mavs', 'Spurs', 'Rockets')
  'Points' = @(25, 13, 36)
  'Assists' = @(7, 9, 12)
}

$team_data = New-Object -Type PSObject -Property $my_data

The following screenshot shows how to use this syntax in practice:

Suppose that we would like to select the property named “Team Name” from the object.

Suppose we attempt to use the following syntax to do so:

$team_data.Team Name

The following screenshot shows how to use this syntax in practice:

Notice that we receive an error since we didn’t use quotes around the property name.

To select the “Team Name” property, we must use the following syntax instead:

$team_data."Team Name"

The following screenshot shows how to use this syntax in practice:

PowerShell select object property name with space

Notice that we’re able to select the property “Team Name” without any errors this time since we wrapped the property name in quotes.

The output displays each of the values for the property “Team Name” from the object named $team_data.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Iterate Over a Hash Table
PowerShell: How to Check if Hash Table Contains Specific Key
PowerShell: How to Check if File Contains Specific String
PowerShell: How to Replace Special Characters in String

Leave a Reply