Question : Powershell script to delete files older than x days

Hi,
I don't have experience with windows scripting and I'm looking for something, that schedule daily, delete files older than x days in a specific folder.

The system runs under windows 2003 R2 (x86).

Thanks in advanced.

Answer : Powershell script to delete files older than x days

Hello :)

In PowerShell make sure that this returns the files you want to remove:


$x = 30 # x days :)
Get-ChildItem "C:\YourFolder" | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$x) }


If it does, add this onto the end:


Get-ChildItem "C:\YourFolder" | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$x) } | Remove-Item


If you want to test it with Remove-Item on as well you'd need this:


Get-ChildItem "C:\YourFolder" | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$x) } | Remove-Item -WhatIf


Remember that files deleted like this won't be going to the recycle bin, so make sure it really only gets what you want first.

HTH

Chris

PS. PowerShell is here if you don't already have it installed: http://support.microsoft.com/kb/968929
Random Solutions  
 
programming4us programming4us