Add New Properties To Your Objects

by Nov 16, 2009

You can use Select-Object to add new properties to an existing object. In PowerShell v.2, Select-Object accepts a wildcard, which in turn adds all existing properties. Why would that be useful? Have a look:

Dir | Select-Object *, Age |
Foreach-Object { $_.Age=(new-timespan $_.LastWriteTime).Days$_ } |
Format-Table Name, Age
Dir | Select-Object *, Age |
Foreach-Object { $_.Age=(new-timespan $_.LastWriteTime).Days$_ } |
Format-Table Name, Age

Twitter This Tip! ReTweet this Tip!