File Age Groupings with PowerShell

I’m always talking about how much the object-nature of PowerShell makes all the difference in the world. Today, I have another example. Let’s say you want to analyze a directory, perhaps a shared group folder for a department. And you want to identify files that haven’t been modified in a while. I like this topic because it is real world and offers a good framework for demonstrating PowerShell techniques.

You would like to divide the files into aging “buckets”. Let’s begin by getting all of the files. I’m using PowerShell 3.0 so you’ll have to adjust parameters if you are using 2.0. You can run all of this interactively in the console, but I think you’ll find using a script much easier.

$files = dir c:\work -recurse -file

Now, let’s add a new property, or member, to the file object called FileAgeDays which will be the value of the number of days since the file was last modified, based on the LastWriteTime property. We’ll use the Add-Member cmdlet to define this property.

$files | Add-Member ScriptProperty -Name FileAgeDays -Value {
 [int]((Get-Date) - ($this.LastWriteTime)).TotalDays }

The new property is technically a ScriptProperty so that we can run a scriptblock to define the value. In this case we’re subtracting the LastwriteTime value of the each object from the current date and time. This will return a TimeStamp object but all we need is the TotalDays property which is cast as an integer, effectively rounding the value. In a pipelined expression like Select-Object you would use $_ to indicate the current object in the pipeline. Here, we can use $this.

Next, we’ll add another script property to define our “bucket” property.

$files | Add-Member ScriptProperty -Name FileAge -Value {
    if ($this.FileAgeDays -ge 365) {
        "1year"
    }
    elseif ($this.FileAgeDays -ge 180) {
        "6Months"
    }
    elseif ($this.FileAgeDays -ge 90) {
        "90Days"
    }
    elseif ($this.FileAgeDays -ge 45) {
        "45Days"
    }
    else {
        "Current"
    }
}

The script block can be as long as you need it to be. Here, we’re using an If/ElseIf construct based on the FileAgeDays property we just created. If we look at $files now, we won’t see these new properties.

fileage-01

But that is because the new properties aren’t part of the default display settings. So we need to specify them.

fileage-02

Now, we can group the objects based on these new properties.

$files | Group FileAge -NoElement | sort Count -Descending

fileage-03Or perhaps we’d like to drill down a bit more.

$grouped = $files | Group FileAge | 
Add-Member -MemberType ScriptProperty -Name SizeMB -Value {
  ($this.Group | Measure-Object Length -sum).Sum / 1MB
} -PassThru

Now we’ve added a new member to the GroupInfo object that will show the total size of all files in each group by MB. Don’t forget to use -Passthru to force PowerShell to write the new object back to the pipeline so it can be saved in the grouped variable. Finally, the result:

$grouped | Sort SizeMB -Descending | Format-Table SizeMB,Count,Name -AutoSize

fileage-04

And there you go. Because we’re working with objects, adding new information is really quite easy. Certainly much easier than trying to do something like this in VBScript! And even if you don’t need this specific solution, I hope that you picked up a technique or two.

About the Author

PowerShell.org Announcer

This is the official account for PowerShell.org and sponsor announcements.