Delete files older than x days in a folder, but IGNORE files in subfolders

So far I have found nothing that addresses my goal. I want to delete all files older than x days with a certain file filter. It will not seem to find ANY files unless I use recurse. But if I use recurse, it includes all files matching the filter in the sub folders. The check to see if it is a container appears to work if you do not want to delete the folder, but it still allows the script to see (and delete) all files matching the date and filter criteria in those subfolders.

Does PS even have a way to ignore files in subfolders that would otherwise match the date and file filter criteria?? I only want to ‘look’ in the root folder.

Below is a script that works IF I user recurse, but it includes all files in the sub folders as well, which I cannot have. Since I had to create the test folders and files, I actually added 1 day so all files would fall within the date range I want to include. Normally, that would be - days instead of + days. I use a variable to hold the child item results to get the count, and whatif to see what would happen. When I do NOT use recurse, the file count changes to 0, though there are a dozen files in the root folder (path) that meet the date and filter criteria. SO with recurse, it includes ALL files even in sub folders, but without recurse, it finds nothing.

$HowOld = +1

#Path to the root folder

$Path = "C:\Users\LANID\Documents\NAS_Cleanup_Test"

$LastWrite = (Get-Date).AddDays($HowOld).ToString('MM/dd/yyyy')

$filtervar = "*.*"

write-output $Path

Write-Output $LastWrite

#Get the files to delete

$filesToDelete = Get-ChildItem -Path $Path -include $filtervar -File -Recurse | Where {($_.creationtime -lt $LastWrite)}


#Delete the files

$filesToDelete | Remove-Item -Force – WhatIf

I’m confused. Do you want to find files even in subfolders or not? If not you should not use the parameter -Recurse. You named your variable to specify the age of the files you’re after “LastWrite” but you’re using the property “creationTime”. Did you mean “LastWriteTime” actually?

BTW: You don’t need to use the method .toString() for your variable $LastWrite. Powershell deals very well with [DateTime] objects and properties.

Learning with the help of community and new to blog.

Tried the below one added -Filter parameter

    $HowOld = -1
#Path to the root folder

$Path = "D:\Powershell\test"

$LastWrite = (Get-Date).AddDays($HowOld)

$filtervar = "*.*"

write-output $Path

Write-Output $LastWrite

#Get the files to delete

$filesToDelete = Get-ChildItem -Path $Path -Filter $filtervar   |

Where-Object {($_.LastWriteTime -lt $LastWrite)}


#Delete the files

$filesToDelete | Remove-Item -Force</pre>

Try adding a * to the end of the path. Get only CSV files in the script directory created within the last 30 days

$files = Get-ChildItem -Path 'C:\Scripts\*' -Recurse -File -Include '*.csv' | 
         Where {$_.LastWriteTime -gt (Get-Date).AddDays(-30)}


Also, please be clear on what the error is (if there is one), what the current results are, what the expected result should be. For instance, I have a scripts directory and a sub-folder Corp. I only want to get text files from the scripts directory and the Corp directory, but there are many sub-directories…

By not knowing what the file type is, I’ll use the csv as an example. Also you did not specify how old the files should be, so let’s make it 3. Path is c:\temp as I had some files there :slight_smile:

The command could be as easy as:

[PRE]Get-ChildItem c:\temp*.csv | where {$_.creationtime -le (get-date).adddays(-3)} | remove-item -WhatIf[/PRE]

In my case returns:

What if: Performing the operation “Remove File” on target “C:\temp\auto.csv”.
What if: Performing the operation “Remove File” on target “C:\temp\Encoding Time.csv”.
What if: Performing the operation “Remove File” on target “C:\temp\psorg.csv”.

Thank you all for your input!!

I have found that our server is using a very old version 2 of ps. I ended up using the “.Name -like”, and it works. Of course when we start running this for real, we will remove -WhatIf :slight_smile:

“$filesToDelete = Get-ChildItem -Path $Path | Where-Object {$.Name -like “*.log”} | Where {($.creationtime -lt $CreateDate)}”

“$filesToDelete | Remove-Item -Force -WhatIf”