Tuesday, January 02, 2007

PowerShell - Delete files based on lastwritetime

One more PowerShell tidbit, then I'm signing off. I just love this shell. I had a need to clean up some old IIS logfiles from a SharePoint portal site, and I wanted to do it by date. I was able, with one command line, remove all 2004 logfiles from the folder with the following command line (split for clarity):

PS> gci -path c:\windows\system32\logfiles\w3svc1
>> where { [datetime]::compare($_.lastwritetime, `
>> [datetime]"1/1/2005") -lt 0 }
>> del
I know, no great shakes in terms of scripting...but there's no really good way to do this from the command shell without resorting to VBScript.

I love this shell!

No comments: