Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2017-10-12
3
Low Priority
?
26 Views
1 Endorsement
Last Modified: 2017-10-12
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.
1
Comment
Question by:goldcream76
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 41

Accepted Solution

by:
footech earned 1000 total points
ID: 42328930
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
 

Author Closing Comment

by:goldcream76
ID: 42329184
Thanks this does exactly what I was asking for
0
 
LVL 8

Expert Comment

by:Senior IT System Engineer
ID: 42329519
Thanks for the script Foo :-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Join & Write a Comment

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

598 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question