December 14th, 2006

File Aging Script

PowerShell Team
PowerShell Team

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

Category
PowerShell

Author

PowerShell Team
PowerShell Team

PowerShell is a task-based command-line shell and scripting language built on .NET. PowerShell helps system administrators and power-users rapidly automate tasks that manage operating systems (Linux, macOS, and Windows) and processes.

0 comments

Discussion are closed.