Renaming Properties (Simple)

by Apr 25, 2023

Select-Object can not just select properties but also rename. Let’s assume you need a list of files in a folder with their size. This line would suffice:

 
PS> Get-ChildItem -Path c:\windows -File | Select-Object -Property Length, FullName 
}

You’d now see just the two selected properties “Length” and “FullName”. But what if you wanted this information to show up with different names, i.e. “Size” and “Path”?

Simply rename the properties by submitting a hash table per property. Inside each hash table, use the keys “Name” to rename the property, and “Expression = ‘[OriginalPropertyName]’ }” to keep the original property content untouched:

 
Get-ChildItem -Path c:\windows -File | Select-Object -Property @{Name='Size';Expression='Length'}, @{Name='Path';Expression='FullName'
 

This way, you can easily rename any property in any object.


Tweet this Tip! Tweet this Tip!