File Aging Script

Jeffery Hicks as a very nice File Aging script.  The thing that Iike about it is that it provides a very good illustration of how to use a number of PowerShell features including:

  • Working with Files

  • Working with Dates

  • Control structures: if, foreach, switch

  • Working with Formating strings

  • Using colorized output

He had one technique that could be improved.  Here is a line from the script: 

$GrandTotal=”{0:N2}” -f ($GrandTotal/1048576)

The question is, what is that number?  Well you might guess that that is 1 MegaByte and you are probably correct but are you sure that a MegaByte isn’t 1045876?  I transpose numbers all the time, maybe Jeffery did as well.  He didn’t but you see the points 1) if it was wrong – would you be able to recognize that it was wrong? 2) if you had to write this yourself – are you going to (a) remember what to type and (b) type it correctly?  You are much better off using our built-in support for “KB”, “MB”, “GB” and make this:

$GrandTotal=”{0:N2}” -f ($GrandTotal/1mb)

Here is a demo:

PS> 1kb
PS> 1mb
PS> 1gb
PS> 1kb+2mb+3gb

Check out Jeffery’s File Aging script at:

Jeffery and Don Jones have been doing lots of great stuff at that web site so you take some time to check it out.

Jeffrey Snover [MSFT]
Windows PowerShell/MMC Architect
Visit the Windows PowerShell Team blog at:
Visit the Windows PowerShell ScriptCenter at: