Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

Hey PowerShell Guy!, How Can I Delete Files That Are a Specified Number of Hours Old?

 And another translation of a Hey Scripting Guy Article : How Can I Delete Files That Are a Specified Number of Hours Old?

dir |? {$_.CreationTime -lt (get-date).AddHours(-8)} | del -whatif

or for files not written in the last 8 hours :

dir |? {$_.LastWriteTime -lt (get-date).AddHours(-8)} | del -whatif

And yes the PowerShell version is with seatbelt included ( Remove -whatIf to really delete the files. )

Need I say more ? 

enjoy,

Greetings /\/\o\/\/

Published Thursday, December 13, 2007 7:03 PM by MoW
Filed under: ,

Comments

# Interesting Finds: Decemer 14, 2007

Friday, December 14, 2007 10:19 AM by Jason Haley

# Interesting Finds: December 14, 2007

Friday, December 14, 2007 10:19 AM by Jason Haley

# re: Hey PowerShell Guy!, How Can I Delete Files That Are a Specified Number of Hours Old?

How would you do this for specified number of days old versus hours?

Tuesday, February 24, 2009 5:04 PM by

# re: Hey PowerShell Guy!, How Can I Delete Files That Are a Specified Number of Hours Old?

I've got a directory from which I need to clear out files AND directories >48hrs old. I want to be able to schedule the clean-out.

I tried your script, & if I don't add -recurse I get prompted about the directories. If I add -recurse, it works, EXCEPT that the directories often contain the file Thumbs.db, which PowerShell tells me either "Not Enough permission to perform operation" (this is logged in as admin on server) or "Cannot remove item...: The process cannot access the file 'Thumbs.db' because it is being used by another process." (which it is NOT being used by anything). Any ideas? I'm pretty much the PowerShell newbie. Thanks.

Tuesday, October 26, 2010 5:19 PM by

# Delete Files Older than (x) Days? Drija

Monday, May 02, 2011 6:13 AM by Delete Files Older than (x) Days? Drija

# Delete Files Older than (x) Days? - Admins Goodies

Monday, August 22, 2011 10:30 PM by Delete Files Older than (x) Days? - Admins Goodies
Anonymous comments are disabled