File Aging Script

File Aging Script

  • Comments 5

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
1024
PS> 1mb
1048576
PS> 1gb
1073741824
PS> 1kb+2mb+3gb
3223323648

Check out Jeffery's File Aging script at: http://sapien.eponym.com/blog/_archives/2006/12/14/2571520.html

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:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 8 and 2 and type the answer here:
  • Post
  • Thanks for the feedback on the formatting. I'm still use to having to do all the thinking in my scripting.  I have to remember the PowerShell has some "smarts" that I can take advantage of. And I do mix up numbers like that all the time and always have to double check. In this case, I used PowerShell to solve the expression 1024*1024 without having to reach for a calculator.

  • Perhaps I'm being pedantic, but I don't think I like this (agreeably handy) little shortcut. I don't believe it adds any clarity to any scripts one may write. The problem I see is two-fold:

    Technically speaking, if I write 1kb, this literally means 1000 bits as adopted by many international standards. (See http://en.wikipedia.org/wiki/Binary_prefix for a discussion). Now 1kb is admittedly more commonly used to designate 1024 bits (or sometimes bytes) and that's fine when precision is not important, but as a constant literal in a programming language, it can lead to confusion and error.

    Also, "kb" seems to imply units (in this case bits), where the quantity may be referring to something else, this can also reduce readability of any scripts.

    I realize that PowerShell is primarily a scripting language where such shortcuts are appreciated - but I would argue that it is also a programming language where precision and clarity are very important.

  • Instead of Kb - Microsoft must use  KiB to respect SI standards - just like they do in Vista now.

  • if ($file.FullName.Length -lt 260)

    Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

  • Use Long Path Tool, it is good for such issues.

Page 1 of 1 (5 items)