Too Old — Your out of here….

Grumpy Admin was asked this morning, if I could do a quick check of any files older the 120 days in the user data area and perhaps delete them and save space on the server! If the users wants them, they can request them to be restored from backup- Cold Storage is much like many of my ex-girlfriends, cold and should never be accessed again! Knowing that it is quite easy to get the Modified: or Accessed: as I wrote a blog about it yesterday, this tasking couldn’t have fallen at a better time for me 🙂 How to make a good impression 101!  So actually it is kind of easy to do and was quick 🙂 Task done before I even finished my first cup of coffee… Might have to drop the grumpy for Grumpy Admin today 🙂

So here is how I did it, we can use the -filter option with in the Where-Object combined with the GCI -Recurse option, basically it just codes itself really 🙂 So first thing we need to do is get the date that we are going to be using to allow us to do comparisons.

So lets use our imagination and create a variable called $Date – Not Naming Variables correctly actually makes me grumpy and makes the code harder to read!

As you should know there is a Get-Date function in Powershell. This is great, but what method are there inside of that function we can use to slove this simple problem – You should know where I am going with this… yes, you guessed it – lets do a

Get-Date | Get-Member



I use Get-Help and Get-Member lots and lots! As you can see from the screen print, was far too lazy this morning to cut & paste – There are quite a few methods available on the Get-Date function. Right at the top – If you see there is an add method, adddays method, but there is no subtract of subtractdays methods…. umm ok… but if you think about it actually very smart.

Why have a subtract function when you can just ADD! For Example 4 + (-2) = 2 So we can make our $Date the current date returned by an offset of 120 days in the past by adding a negative value to it! So our line will look like this

$Date = (Get-Date).AddDays(-120)

Excellent – We already decided that we are going to be using GCI (Get-ChildItem) so we can construct a line like this :-

Get-ChildItem -Path D:\Data\ -Recurse -File

This will return every file in the D:\Data which is a good start so let now | that to our Where-Object and use the -Filter option. So our code will now look like this!

$Date = (Get-Date).AddDays(-120)
Get-ChildItem -Path D:\Data\ -Recurse -File | Where-Object -Filter { $_.LastAccessTime -lt $Date}

Simple, now you can do what you want with the output – For Example, you can use the Export-CSV or you could do something evil like | remove-item 🙂 That good way of starting the day, I wonder what command Grumpy Admin put to at the end lol

This was just a quick and dirty Powershell command, this has a function written all over it! Maybe I will turn this into a function at some point in the future – or you guys can – Why not have a go?


Leave a Reply

Your email address will not be published. Required fields are marked *