As Andrew Morgan provided me a good comment in one of my previous posts ( http://jeffwouters.nl/index.php/2011/10/powershell-script-to-delete-files-older-that-a-week/ ), I changed the script a little.
The previous script deleted files created more than 7 days ago, but didn’t care if the files were accessed yesterday or not. This was intended for that specific script and situation, but his command was valid for cases where a company policy slightly differs from the policy in my case.
In my case, all files were to be deleted if they were 7 days or older.
In the new case, all files were to be deleted if they were not accessed for 7 days or longer.
Here’s the new code:
$TargetFolder = “D:\Company\DropBox”
foreach ($i in Get-ChildItem $TargetFolder -recurse)
{
if ($i.LastWriteTime -lt ($(Get-Date).AddDays(-7)))
{
Remove-Item $File.FullName -force
}
}
And if you only want to include specific extensions, let’s say for example .html and .txt, a little addition to the script is required:
$TargetFolder = “D:\Company\DropBox”
foreach ($i in Get-ChildItem $TargetFolder -recurse -include *.txt, *.html)
{
if ($i.LastWriteTime -lt ($(Get-Date).AddDays(-7)))
{
Remove-Item $File.FullName -force
}
}

Categories
Tag Cloud
Blog RSS
Comments RSS
Last 50 Posts
Back
Void « Default
Life
Earth
Wind
Water
Fire
Light 