Pages

Wednesday, 8 February 2012

Deleting files by age with PowerShell

Back in November 2011 I blogged about using FORFILES to delete files by age.  I've since been "playing" with PowerShell and intend to blog quite a bit about it over the coming months.

To say PowerShell is powerful is an understatement!  There's plenty of material on the Internet to help you too.  But I wanted to kick off by replacing that old article with a PowerShell script, basically because I find PowerShell far easier to read!

But rather than just dumping a command with little explanation, I want to work you through it. This also demonstrates how I think through a problem with PowerShell and maybe that'll help you too. So I start with trying to display the items I want to deal with (be it report on, delete, move, action etc):
Get-ChildItem "C:\Example"

That will list all the files in C:\Example directory. A bit like this:
Mode                LastWriteTime     Length Name                                                                       
----                -------------     ------ ----                                                                       
-a---       7/02/2012  11:41 p.m.        598 File A.txt                                                                 
-a---       7/02/2012  11:41 p.m.         95 File B.txt                                                                 
-a---       7/02/2012  11:41 p.m.        311 File C.txt                                                                 
-a---       7/02/2012  11:41 p.m.         12 File D.dat   

Note: You can use DIR too as it is aliased to Get-ChildItem.

Ok...but we're interested in files older than an age.
So we might come up with something like this:
Get-ChildItem "C:\Example" | Where-Object {
   $_.CreationTime -le [datetime]"2012-02-01 00:00:00"
}

That shows just one file:
Mode                LastWriteTime     Length Name                                                                       
----                -------------     ------ ----                                                                       
-a---       7/02/2012  11:41 p.m.        598 File A.txt  

But we cant really verify thats the only file older than the date specified. If you wanted to show the CreationTime you'd need to adjust the command to include a "Select-Object" such as this:
Get-ChildItem "C:\Example" | Where-Object {
   $_.CreationTime -le [datetime]"2012-02-01 00:00:00"
} | select Mode, LastWriteTime, CreationTime, Length, Name
That would show:
Mode                     LastWriteTime           CreationTime                             Length Name                   
----                     -------------           ------------                             ------ ----                   
-a---                    7/02/2012 11:41:02 p.m. 31/01/2012 7:30:34 a.m.                     598 File A.txt   

So to do the delete? Simple, just pipe into the Remove-Item cmdlet:
Get-ChildItem "C:\Example" | Where-Object {
   $_.CreationTime -le [datetime]"2012-02-01 00:00:00"
} | Remove-Item

And that's pretty much it!

But wait, there's more. Lets say we wanted to do something like "Show all files older than one month". We employ the Get-Date cmdlet and one of its methods:
Get-ChildItem "C:\Example" | Where-Object {
   $_.CreationTime -le (Get-Date).AddMonths(-1)
}

Want to know what other methods Get-Date offers? Try this:
Get-Date | Get-Member

No comments:

Post a Comment