Thursday 28 February 2008

My First PowerShell Script - delete files which haven't been accessed in 30 days

I thought I'd start with something simple.
I have a Scratch folder on my Windows desktop where I tend to chuck things I only need for a short while - like if someone sends me a file they're having problems with and they want me to figure out what's wrong with it.
Every now and again I notice my Scrach folder is getting cluttered with stuff I don't need, and at that point I sort it by Last Access Date and delete everything I haven't looked at for a month or so.
I'm no good at learning scripting from a book, every scripting and macro language so far I've learnt through necessity - I had a job that needed doing and I didn't know any other way to do it, so I taught myself. I've been waiting for an opportunity to try to get to grips with PowerShell and I thought this would be a great project to start with.
Note that there's no safety net here - running this script deletes all files and subfolders that meet its criteria and once they're gone they're gone! As there's every chance I've made an error here or this may behave differently when you run it, see my disclaimer before you even think about running it!
$userprofile=$env:UserProfile
set-location "$userprofile\desktop\Scratch"
$d=[DateTime]::Now.AddDays(-30)
Get-childitem -recurse | where-object {$_.lastaccesstime -lt $d} | remove-item -recurse -force
Get-ChildItem -recurse | where-object {$_.psiscontainer -and ($_.getfiles().length -eq 0)} | Remove-Item –recurse -force
Notes for those even less PowerShelly than me:
AddDays(-30) subtracts 30 days. (There isn't a separate SubtractDays method.)
The first Get-Childitem line finds all the files and subfolders that haven't been accessed for 30 days and deletes them.
The next Get-ChildItem line finds all the empty subfolders (getfiles().length will be 0 if there are no files in the subfolder) and deletes them. (This took me a while to figure out but I was pretty pleased when I managed it - I'd been looking around couldn't find any examples of a one-liner to delete empty folders.)
Update 29-Feb: For reasons unknown the formatting of the above script had all gone strange when I came back to it today. Hopefully it looks good now.

1 comment:

  1. Congratulations on your first PowerShell Script. I hope you like it enough to write a couple thousand more.

    RE: No safety net.
    Remove-Item can take a -CONFIRM or a -WHATIF parameter so you can provide a safety net if you want to.

    Cheers!

    PS: Don't forget to tell us what you think we got wrong so we can address it in future releases.

    Jeffrey Snover [MSFT]
    Windows Management Partner Architect
    Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
    Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

    ReplyDelete

Creative Commons License This work by TechieBird is licensed under a Creative Commons Attribution-No Derivative Works 2.0 UK: England & Wales License.