Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7078253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:31:29+00:00 2026-05-28T06:31:29+00:00

I asked a question earlier about how to go through a directory structure and

  • 0

I asked a question earlier about how to go through a directory structure and keep the 7 most recent copies of a file in each sub directory. The script below is what was created and it works great but only on 1 directory. I need to to go through the entire directory and keep the 7 newest files in each sub directory.

To test I created a directory structure under C:\Customer with sub directories of Test1, Test2, Test3. I then created 12 test files in each directory including the top level C:\Customer. The files were named Test1_Report_.txt – Test12. When I ran the script below it deleted all the files in the top level, Test1, Test2 and kept the 7 most recent copies in Test3. What am I missing here? Any help would be greatly appreciated.

$path = "C:\Customer"


$files = Get-ChildItem -Path $path -Recurse | Where-Object {-not $_.PsIsContainer} |where{$_.name -like "*_Report_*"}
$keep = 7
if ($files.Count -gt $keep) {
    $files |Sort-Object CreationTime |Select-Object -First ($files.Count - $keep)| Remove-Item -Force
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:31:29+00:00Added an answer on May 28, 2026 at 6:31 am

    Untested but I think this will do what you need. This will process all directories and keep the 7 most recent files in each sub directory.

    $path = "C:\Customer"
    $keep = 7
    
    $dirs = Get-ChildItem -Path $path -Recurse | Where-Object {$_.PsIsContainer}
    foreach ($dir in $dirs) {
        $files = Get-ChildItem -Path $dir.FullName | Where-Object {-not $_.PsIsContainer -and $_.name -like "*_Report_*"}
        if ($files.Count -gt $keep) {
            $files | Sort-Object CreationTime | Select-Object -First ($files.Count - $keep) | Remove-Item -Force
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Earlier I asked a question about command-line parameters to automate processing of a file
Earlier, I asked a question about getting windows active directory information from the user
I asked a question earlier about which language to use for an AI prototype.
I asked a question earlier about why left joins in Linq can't use defined
I asked a question earlier about keyhooks in vb.net. My current issue is such:
I asked a question earlier about ESBs written in Erlang or Java, and there
Ok, I asked a question earlier about Flex and ADO.NET Data Services but didn't
This is related to this question I asked earlier about syntax highlighting user-defined blocks
Earlier I asked a question about why I see so many examples use the
Earlier today I asked a question about environ , and one of the more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.