Adding File Age to file objects

by Dec 14, 2009

PowerShell cmdlets return objects with rich information, and to see all the information, you can pipe the result to Format-List *:

Dir $env:windir | Format-List *

You can easily append more properties if the information returned is not sufficient. For example, open Notepad and enter this to add a property called "Age" that returns the age in days for a file:

(new-timespan $this.LastWriteTime).Days

Save it as fileage.ps1xml. Next, update PowerShell using this command:

Update-TypeData c:\…\fileage.ps1xml

Once done, you can show the Age column and even use it to retrieve old or brand new files whenever you list files:

dir $env:windir | Format-Table Name, Age

The next line selects recursively all log files in your Windows folder, which have been modified within the last 30 days:

dir $env:windir filter *.log -rec |
Where-Object { $_.Age -lt 30 } |
Format-Table FullName, Age

Twitter This Tip! ReTweet this Tip!