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 7594821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:31:01+00:00 2026-05-30T21:31:01+00:00

Trying to educate myself: when processing a whole tree of items from get-childitem -recurse

  • 0

Trying to educate myself: when processing a whole tree of items from

get-childitem -recurse

I often have to do it from the leaf level up to the top level (for example, deleting files and folders, it’s problemmatic to modify the upper levels of the tree and then attempt to deal with the lower levels.

To work around this, I’ve resorted to sorting the whole collection by the number of delimiters in the paths, to get the leaf level first, followed by upper levels of the tree:

$someFiles = Get-ChildItem -Recurse |
Sort @{Expression={ ( $_.fullname | select-string "\\" -AllMatches ).matches.count };
Descending=$true }

This feels wrong – I have limited coding experience, but I know that if I just traverse the tree in the right way, this expensive and goofy sort should not be necessary. But -recurse is so handy!

What’s the smarter way to do this? To be specific, is there a way to traverse the tree with get-childitem, from leaf up, that does not require sorting all the results?

  • 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-30T21:31:03+00:00Added an answer on May 30, 2026 at 9:31 pm

    You could avoid the regular expression and just use the string split method. It also performs better.

    dir -recurse | sort -Property @{ Expression = {$_.FullName.Split('\').Count} } -Desc
    

    Result:

    TotalMilliseconds : 346.1253
    

    vs…

    Get-ChildItem -Recurse | Sort @{Expression={ ( $_.fullname | select-string "\\" -AllMatches ).matches.count }; Descending=$true }
    

    Result:

    TotalMilliseconds : 953.6606
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to solve some queries from sql zoo to practice sql skills I have
I am a newbie for WCF, so please educate me. I am trying to
Trying to get a wildcard search to pick up on any text in org_name
trying to figure out why this is happening - I have an input text
Basically I have an iframe loaded that is accessed from the parent whenever it
In Java I'm trying to test for a null value, from a ResultSet, where
We are trying to have an online handwritten text recognizer for the Android platform.
I am trying to build a basic plugin system like the kind you often
Trying to pull a value from the sunrise element from [yweather:astronomy] in the Yahoo
Trying to read registry keys remotly (from a host on local intranet)... All the

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.