Wednesday, February 13, 2013

Powershell Script DIY: removing items based on date


Powershell Script DIY: removing items based on date
Warning: This DYI is potentially damaging, be absolutely sure you want to do this and you test it in a sandbox first.
With that being said, today we are going to remove items from a directory basted on their date.  The first thing we are going to do is define the directory.  We start with “dir”:

                Get-ChildItem

We need to define the location:

                Get-ChildItem 'C:\Temp\'

Now we use the –recurse operator.  Recurse is short for recursive which, plainly put means serch everything in subdirectories too.  So if you don’t want it to do this, skip this next step:

                Get-ChildItem 'C:\Temp\'-recurse 

Now that we have defined the scope of the commands, we need to use the pipe operator in order to tell it “now that you have gotten c:\Temp AND everything in every folder it contains do this”:

                Get-ChildItem 'C:\Temp\'-recurse  |

In order to diferenciate between files, we need to give Powershell a conditional statement.  We begin this with “where”:

                Get-ChildItem 'C:\Temp\'-recurse  | where

When you use a conditional statement, such as foreach-Item or Where, we need to encapsulate the statement itself in curley brackets {}:

                Get-ChildItem 'C:\Temp\'-recurse  | where {}

Because we want to remove old items we are going to use the get-date command:

                Get-ChildItem 'C:\Temp\'-recurse  | where {(get-date)}

We encapsulate this in parantheses as well in order to say that we are not getting the date from system time.  Next we define what date we want to get, which is the file creation time time:

                Get-ChildItem 'C:\Temp\'-recurse  | where { ((get-date)-$_.creationTime)

Remember the -$_. The – indicates what to get, the $_ indicates it is a list of items and the . belongs to .creationTime.  Moving along we encapsulated all of that within another set of parentheses to say that is a group that has a specific output, creation time. We need to then do something with it to indicate the length of time that has passed since it was created.  This is where we use .days:

                Get-ChildItem 'C:\Temp\'-recurse  | where { ((get-date)-$_.creationTime).days

Now here is where we accually select old items with the –ge operator.  Remember if the file was created today, then its age is 0.  For our purpouses we will say we want to select all files not created today:

                Get-ChildItem 'C:\Temp\'-recurse  | where { ((get-date)-$_.creationTime).days -ge 0

Now that we have selected the files that we want we need to add an additional command, which means an additional Pipe operator:

                Get-ChildItem 'C:\Temp\'-recurse  | where { ((get-date)-$_.creationTime).days -ge 0} |

Now because we want to remove we use the remove-item command, which was the same as del in DOS:

                Get-ChildItem 'C:\Temp\'-recurse  | where { ((get-date)-$_.creationTime).days -ge 0} | remove-item

Remove-Item has a couple of switches in it that you can use, for our purposes we are going to use the –force and the –recurse switches to tell it to delete EVERYTHING selected and to do it nondescrimitory:

                Get-ChildItem 'C:\Temp\'-recurse  | where { ((get-date)-$_.creationTime).days -ge 0} | remove-item -force –recurse

And there you have it!  As usual please add your comments below and I fully encourage you to share this with your colleagues.  Also that extra few clicks to +1, Like, Tweet, linkedin Goes a long way for me!

Sources: Microsoft Scripting guy http://preview.tinyurl.com/37ljrya, Myself

No comments:

Post a Comment