Adding File Age to file objects

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:

<Types>
<Type>
<Name>System.IO.FileInfo</Name>
<Members>
<ScriptProperty>
<Name>Age</Name>
<GetScriptBlock>
(new-timespan $this.LastWriteTime).Days
</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>
</Types>

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!


Posted Dec 14 2009, 08:00 AM by ps1

Comments

Twitter Trackbacks for Adding File Age to file objects - Power Tips - PowerShell.com [powershell.com] on Topsy.com wrote Twitter Trackbacks for Adding File Age to file objects - Power Tips - PowerShell.com [powershell.com] on Topsy.com
on 12-14-2009 10:29 AM

Pingback from  Twitter Trackbacks for                 Adding File Age to file objects - Power Tips - PowerShell.com         [powershell.com]        on Topsy.com

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.