Need a powershell script to delete files 45 days or older in path recursively but only in subfolders named "common"

We have a Directory structure like below

C:\Clients\Client1
                        -User
                       -System
                       -Common
                                 -FolderA
                                 -FolderB
                                 -FolderC
                  \Client2
                       -User
                       -System
                       -Common
                               -FolderA
                               -FolderB
                               -FolderC

I need a script that can recursively look through the C:\Clients\ folder and locate the "common" folder in each Clients subfolder and delete all files with a creation date of 45 days or older  in the "Common" directory and all subfolders in that directory.   It needs to leave all files in the User and System folders and subfolders alone.  I've tried various things but my powershell scripting is really limited and the best I've been able to accomplish is a powershell script that deletes all files 45 days or older and leaves the actual system and users folder alone but still deletes files in subfolders of system and users.   Hope that all makes sense....any help is appreciated...Thanks.
goldcream76Asked:
Who is Participating?
 
footechCommented:
Always best to show what you have so that those who might try to help don't have to recreate what might already be working (it also shows that you've put some actual effort into the issue and are invested).
However, this isn't too complex...
$cutoff = (Get-Date).AddDays(-45).Date
Get-ChildItem -Path C:\Clients -Filter "Common" -Recurse -Directory | ForEach `
{
    Get-ChildItem -Path $_.FullName -File -Recurse |
     Where { $_.CreationTime -lt $cutoff }
} | Remove-Item -WhatIf

Open in new window

Remove the -whatif parameter to run for real.
1
 
goldcream76Author Commented:
Thanks this does exactly what I was asking for
0
 
Senior IT System EngineerIT ProfessionalCommented:
Thanks for the script Foo :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.