PowerShell for Windows Admins

Sep 12 2010   5:52AM GMT

Deleting files older than a certain date



Posted by: Richard Siddaway
File System, PowerShell v2

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

Leave a comment: