PowerShell: How to Use Select-Object and Rename Property


Often you may want to use the Select-Object cmdlet in PowerShell to select a specific property of an object and then rename the property in the output.

You can use the following basic syntax to do so:

$my_data | Select-Object team, @{Name='TotalPoints'; Expression='points'}

This particular example selects the team and points properties from the $my_data object.

It then renames the points property to TotalPoints in the output.

Note that you can also use the following syntax for shorter code:

$my_data | Select-Object team, @{n='TotalPoints'; e='points'} 

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

Example: Use Select-Object and Rename Property in PowerShell

Suppose we have a CSV file located at the following path:

  • c:\users\bobbi\nba_data.csv

Suppose we use the Import-Csv cmdlet to import this CSV file into PowerShell:

$my_data = Import-Csv c:\users\bobbi\nba_data.csv

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

The $my_data object has three properties: team, points and assists.

Suppose that we would like to select the team and points columns and rename the points column to TotalPoints in the output.

We can use the following syntax to do so:

$my_data | Select-Object team, @{Name='TotalPoints'; Expression='points'}

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

PowerShell Select-Object and rename property

Notice that this returns the team column along with the points column renamed as TotalPoints.

Also note that we could use the following syntax to get the same results:

$my_data | Select-Object team, @{n='TotalPoints'; e='points'}

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

Notice that this returns the same results as the previous example.

Related Tutorials

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

PowerShell: How to Use Select-Object to Only Get Property Value
PowerShell: How to Use Group-Object with Multiple Properties
PowerShell: How to Use Sort-Object with Multiple Properties

Leave a Reply