user3355961
user3355961

Reputation: 725

Deleting Files Older than 30 Days with Powershell

I am trying to delete all files in a directory and all files in its sub-directories older than 30 days, leaving all folders intact. This question seems to have been asked to death online and I have this solution which i got from Stackoverflow:

$limit = (Get-Date).AddDays(-30)
$path = "path-to"

# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

Now this works and it doesn't. When I try this on certain directories it works fine and exits normally. But when I try it on others I get this error:

Get-ChildItem : The given path's format is not supported.
At C:path-to-whatever\ClearFiles.ps1:5 char:1
+ Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsCo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], NotSupportedException
    + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.GetChildItemCommand

I assume this is because of the time format in $._CreationTime, I have tried to remove this but when I do it continually asks me if i really want to delete the following files because I have not specified the recursive parameter, which I have at the beginning.

Could anyone clear this up? And perhaps explain why it works on some directories and not others.

Cheers

Upvotes: 0

Views: 9999

Answers (1)

Zach Olinske
Zach Olinske

Reputation: 557

I couldn't reproduce your issue with the following code, but I will explain how I did it with some error handling ideas.

Lets first compare the two variables.

$_.LastWriteTime = Last time the file was written to. $_.CreationTime = Time the file was created or Copy and pasted.

Adding the Out-GridView with the Select statement will provide us with a list of files on that path. I have added the Name, Attributes, CreationTime, LastWriteTime, and Fullname.

Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.CreationTime -lt $Date } | Select Name, Attributes, CreationTime, LastWriteTime, Fullname | Out-GridView

If you Run As Administrator you could see more files. Certain hidden directories requires Run As Administrator.

Remove-Item has a really nice option of -WhatIf. What if we decide to delete the folders and files. WhatIf option doesn't delete, but it will show you what would have been deleted. Great for testing.

Get-ChildItem -Path $path -Recurse  | Where-Object { $_.CreationTime -lt $Date } | Remove-Item -Recurse -whatif

Lets put this into a working Function with some error handling:

Function Remove_FilesCreatedBeforeDate{
$Path="F:\ISO\"
$Date=(Get-Date).AddDays(-30)
$ValidPath = Test-Path $Path -IsValid
If ($ValidPath -eq $True) {
Write-Host "Path is OK and Cleanup is now running"
#Get-ChildItem -Path $path -Recurse | Where-Object { $_.CreationTime -lt $Date } | Select Name, Attributes, CreationTime, LastWriteTime, Fullname | Out-GridView
#Get-ChildItem -Path $path -Recurse | Where-Object { $_.CreationTime -lt $Date } | Remove-Item -Recurse -whatif
Get-ChildItem -Path $path -Recurse  | Where-Object { $_.CreationTime -lt $Date } #| Remove-Item -Recurse -Verbose
}
Else {Write-Host "Path is not a ValidPath"}
}
    Remove_FilesCreatedBeforeDate

You only see the Warning\Confirm menu when you are about to delete a folder structure. What a lot of people fail to understand is the -Force option only removes hidden files and read-only files. We will want to use the -Recurse option to avoid this prompt, but note that it will delete everything.

I have commented out the Remove-Item for safety reasons. #| Remove-Item -Recurse -Verbose

This works with $Path options like \\SERVER\$C\Directory\ or C:\Directory\. Let me know if you have any issues with this function.

Upvotes: 1

Related Questions