PowerShell for Windows Admins

Sep 12 2010   5:52AM GMT

Deleting files older than a certain date

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

A question on the ITKE forums asked how files older than a certain date (in this case two months) could be deleted without touching younger files.

001
002
003
$date = (Get-Date).AddMonths(-2)
Get-ChildItem -Path c:\scripts | where {!$_.PSIsContainer} |
foreach {if ($_.LastWriteTime -lt $date){Remove-Item $_ -whatif}}

Simply remove the –whatif parameter for the delete to actually happen

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: