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

  • Home
  • SEARCH
  • 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 8513029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:26:32+00:00 2026-06-11T04:26:32+00:00

my problem is with Powershell. I have a very big Folder. Insider are about

  • 0

my problem is with Powershell.
I have a very big Folder. Insider are about 1 600 000 Subfolders.
My task is to erase all empty folders or files beneath them which are older than 6 months.
I wrote a loop with foreach but it takes ages before powershell starts with it ->

…

foreach ($item in Get-ChildItem -Path $rootPath -recurse -force | Where-Object -FilterScript { $_.LastWriteTime -lt $date })
{
# here comes a script which will erase the file when its older than 6 months
# here comes a script which will erase the folder if it's a folder AND does not have child items of its own

…

The Problem: my internal memory gets full(4GB) and i cant properly work anymore.
My guess: powershell loads all 1 600 000 folders, and only after that it begins to filter them.

Is there a possibility to prevent this?

  • 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-06-11T04:26:34+00:00Added an answer on June 11, 2026 at 4:26 am

    You are correct, all 1.6M folders, or at least references to them, are being loaded at once. Best practice is to filter left & format right; IOW, remove those folders before you hit Where-Object if at all possible (unfortunately, gci doesn’t support a date filter AFAICT). Also, if you keep things in the pipeline, you’ll use less memory.

    The following will restrict $items to only those folders which match your criteria, then perform your loop over those objects.

    $items = Get-ChildItem -path $rootpath -recurse -force | ?{ $_.LastWriteTime -lt $date }
    foreach ($item in $items) {
    # here comes a script which will erase the file when its older than 6 months
    # here comes a script which will erase the folder if it's a folder AND does not have child items of its own
    }
    

    Or streamlining further:

    function runScripts {
        # here comes a script which will erase the file when its older than 6 months. Pass $input into that script. $input will be a folder.
        # here comes a script which will erase the folder if it's a folder AND does not have child items of its own Pass $input into that script. $input will be a folder.
    }
    Get-ChildItem -path $rootpath -recurse -force | ?{ $_.LastWriteTime -lt $date }|runScripts
    

    In this last case, you’re using runScripts as a function which uses the pipelined object as a parameter which can be operated on ($input), so you can send everything through the pipeline instead of using those intermediate objects (which will consume more memory).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently exploring powershell capabilities, but I have encountered a problem that I have
I am have a powershell script that does a few things that all need
I am not very familiar with powershell scripting and I'm stuck on this problem
I have a slight problem with Powershell strings. There is a Java program I'm
I'm trying to set up a PowerShell profile so all of my machines have
Have a very simple powershell script to count the number of trades in a
I'm attempting to run a powershell script from C#. I have no problem passing
I have a couple of powershell command that are very simple. disable-mailbox dadelgad -confirm:$false
I'm running into an interesting problem in Powershell, and haven't been able to find
There seems to be a problem when virtualenv is used in PowerShell. When I

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.