List folders at or below a given depth in Powershell2019 Community Moderator ElectionDetermine installed PowerShell versionHow to run a PowerShell scriptCopy-Item problem with destinationPowerShell says “execution of scripts is disabled on this system.”Comparing Sub-Folders and Copying Files with PowerShellHow do you comment out code in PowerShell?C# recursively go through items in a List of T where T has a List of TUsing Get-ACL to get file share permissionsPowerShell - Determine the existence of certain files in a folder hierarchy efficientlyHow to remove intermediate folders containing only one folder each?

Use of undefined constant bloginfo

Why is the President allowed to veto a cancellation of emergency powers?

What options are left, if Britain cannot decide?

In a future war, an old lady is trying to raise a boy but one of the weapons has made everyone deaf

How Could an Airship Be Repaired Mid-Flight

Existence of subset with given Hausdorff dimension

Does Mathematica reuse previous computations?

Are there other languages, besides English, where the indefinite (or definite) article varies based on sound?

How to terminate ping <dest> &

Why do passenger jet manufacturers design their planes with stall prevention systems?

How to explain that I do not want to visit a country due to personal safety concern?

Official degrees of earth’s rotation per day

Is it normal that my co-workers at a fitness company criticize my food choices?

Gravity magic - How does it work?

How to make healing in an exploration game interesting

How to read the value of this capacitor?

Why doesn't the EU now just force the UK to choose between referendum and no-deal?

Sailing the cryptic seas

Can a druid choose the size of its wild shape beast?

Is there a data structure that only stores hash codes and not the actual objects?

How could a scammer know the apps on my phone / iTunes account?

My Graph Theory Students

How do I hide Chekhov's Gun?

how to write formula in word in latex



List folders at or below a given depth in Powershell



2019 Community Moderator ElectionDetermine installed PowerShell versionHow to run a PowerShell scriptCopy-Item problem with destinationPowerShell says “execution of scripts is disabled on this system.”Comparing Sub-Folders and Copying Files with PowerShellHow do you comment out code in PowerShell?C# recursively go through items in a List of T where T has a List of TUsing Get-ACL to get file share permissionsPowerShell - Determine the existence of certain files in a folder hierarchy efficientlyHow to remove intermediate folders containing only one folder each?










2















I have a directory which contains a lot of folders. I want to list all folder (path) that go deeper than 2 levels. So in below case folder 1 & 2.



Directory/folder1
Directory/folder1/test1/test/testsub
Directory/folder1/test2
Directory/folder1/test3
Directory/folder2/blablabla/bla/1
Directory/folder3/test
Directory/folder4/test
Directory/folder5/test


I was trying the following:



$Depth = 3
$Path = "."

$Levels = "*" * $Depth
$Folder = Get-Item $Path
$FolderFullName = $Folder.FullName
Resolve-Path $FolderFullName$Levels | Get-Item | ? $_.PsIsContainer | Write-Host









share|improve this question




























    2















    I have a directory which contains a lot of folders. I want to list all folder (path) that go deeper than 2 levels. So in below case folder 1 & 2.



    Directory/folder1
    Directory/folder1/test1/test/testsub
    Directory/folder1/test2
    Directory/folder1/test3
    Directory/folder2/blablabla/bla/1
    Directory/folder3/test
    Directory/folder4/test
    Directory/folder5/test


    I was trying the following:



    $Depth = 3
    $Path = "."

    $Levels = "*" * $Depth
    $Folder = Get-Item $Path
    $FolderFullName = $Folder.FullName
    Resolve-Path $FolderFullName$Levels | Get-Item | ? $_.PsIsContainer | Write-Host









    share|improve this question


























      2












      2








      2


      1






      I have a directory which contains a lot of folders. I want to list all folder (path) that go deeper than 2 levels. So in below case folder 1 & 2.



      Directory/folder1
      Directory/folder1/test1/test/testsub
      Directory/folder1/test2
      Directory/folder1/test3
      Directory/folder2/blablabla/bla/1
      Directory/folder3/test
      Directory/folder4/test
      Directory/folder5/test


      I was trying the following:



      $Depth = 3
      $Path = "."

      $Levels = "*" * $Depth
      $Folder = Get-Item $Path
      $FolderFullName = $Folder.FullName
      Resolve-Path $FolderFullName$Levels | Get-Item | ? $_.PsIsContainer | Write-Host









      share|improve this question
















      I have a directory which contains a lot of folders. I want to list all folder (path) that go deeper than 2 levels. So in below case folder 1 & 2.



      Directory/folder1
      Directory/folder1/test1/test/testsub
      Directory/folder1/test2
      Directory/folder1/test3
      Directory/folder2/blablabla/bla/1
      Directory/folder3/test
      Directory/folder4/test
      Directory/folder5/test


      I was trying the following:



      $Depth = 3
      $Path = "."

      $Levels = "*" * $Depth
      $Folder = Get-Item $Path
      $FolderFullName = $Folder.FullName
      Resolve-Path $FolderFullName$Levels | Get-Item | ? $_.PsIsContainer | Write-Host






      powershell recursion depth folder-structure






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 6 at 21:34









      mklement0

      135k22252289




      135k22252289










      asked Mar 6 at 17:58









      NickNick

      424




      424






















          2 Answers
          2






          active

          oldest

          votes


















          1














          The solution immediately below, which builds on your own, assumes that your intent is to find those child directories whose subtrees exceed a given depth.



          If you instead want to find all directory paths that are at a given depth or deeper, see the bottom section.

          Your approach cannot achieve that, because it finds directories at the given depth only, not also below.




          Your own clever wildcard-based approach should work in principle, but:



          • (a) it can be greatly streamlined.


          • (b) additional work is needed to limit output to the distinct list of those child folders whose subtrees are too deep.


          (a) Streamlining your approach:



          $Depth = 3
          $Path = '.'

          $Levels = '/*' * $Depth
          Get-ChildItem -Directory $Path/$Levels


          • As in your own approach, '/*' * $Depth dynamically creates a multi-directory-level wildcard expression (e.g., /*/* for a $Depth of 2) that can be appended to the input $Path to match only paths at that level.


          • The -Directory switch (PSv3+) limits matching to directories only.


          (b) Limiting output to the distinct set of top-level folder with too-deep subtrees:



          $Depth = 3
          $Path = '.'

          $Levels = '/*' * $Depth
          Get-ChildItem -Directory $Path/$Levels |
          ForEach-Object ($_.FullName -split '[\/]')[-$Depth] |
          Select-Object -Unique


          Note: Splitting by [/\] - that is, by either / or - makes the solution work on Unix-like platforms too (PowerShell Core); on Windows, -split '\' (by an escaped ) is sufficient.



          With your sample folder hierarchy, the above would yield:



          folder1
          folder2


          • If you want the full paths instead, append
            | Convert-Path -LiteralPath "$Path/$_" .


          • If you want directory-info objects ([System.IO.DirectoryInfo]) instead, append
            | Get-Item -LiteralPath "$Path/$_" .



          Optional reading: Getting folders up to, at, or beyond a certain depth:



          Note:



          • Even though the solutions below target folders (directories), you can include files too by simply omitting -Directory, or target files only by replacing -Directory with -File.


          • For simplicity, the commands implicitly target the current directory.



          At-a-given-depth-only logic:



          This is the same logic employed in the solution above; the following code lists folders at depth 2 only, i.e. those at the grandchild level (child directories of child directories) - note that, unlike with Get-ChildItem -Depth, depth counting starts with 1, i.e. 1 refers to child directories:



          $depth = 2 # grandchild directories only

          Get-ChildItem -Directory -Path ('*/' * $depth)


          • To output full paths, enclose the Get-ChildItem command in (...).FullName or pipe it to Select-Object -ExpandProperty FullName.


          • To output relative paths (e.g., folder1/test1/test/testsub), additional work is needed, because adding -Name will not work as expected in this case (it will output just the directory names):


          $depth = 2 # grandchild directories

          # Calculate the length of the path prefix for determining relative paths.
          # (Using the current dir ($PWD) as the reference path here.)
          $PrefixLen = (Convert-Path -LiteralPath $PWD).Length + 1

          $Levels = '/*' * $Depth
          Get-ChildItem -Directory -Path ('*/' * $depth) |
          ForEach-Object $_.FullName.Substring($PrefixLen)


          Up-to-a-given-depth logic:



          The PSv5+ -Depth parameter limits Get-ChildItem's recursion depth, i.e., it only finds items up to the specified depth, but note that it is depth 0, not 1 that represents the immediate children.

          Note that use of -Depth implies -Recurse, though you may specify the latter as well.



          For instance, to enumerate child folders and grandchild folders (2 levels) in the current directory, use:



          $depth = 2 # child and grandchild directories

          Get-ChildItem -Directory -Depth ($depth - 1)


          • To output full paths, enclose the Get-ChildItem command in (...).FullName or pipe it to Select-Object -ExpandProperty FullName.


          • To output relative paths, simply add the -Name switch to the Get-ChildItem call.


          At-a-given-depth-or-deeper logic:



          Limiting results to items at levels greater than or equal to a given depth requires a custom solution:



          $depth = 2 # grandchild directories and below

          Get-ChildItem -Directory -Name -Recurse |
          Where-Object ($_ -split '[/\]').Count -ge 2 |
          Get-Item -LiteralPath "$PWD/$_"


          If your input path isn't the (implied) current dir., substitute that path for $PWD.



          • To output full paths, replace Get-Item with Convert-Path.


          • To output relative paths, simply omit the Get-Item call.






          share|improve this answer
































            -1














            Please try this



            $Folders = Get-ChildItem 'C:Program Files' -Directory -Recurse -Depth 1 | 
            Select-Object -ExpandProperty fullname |
            Sort-Object
            $Folders





            share|improve this answer


















            • 1





              -Depth 1 will limit the recursion to the first 2 levels, so you won't know which directories have subtrees that go deeper, which is what the OP is after.

              – mklement0
              Mar 6 at 18:35










            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55029472%2flist-folders-at-or-below-a-given-depth-in-powershell%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The solution immediately below, which builds on your own, assumes that your intent is to find those child directories whose subtrees exceed a given depth.



            If you instead want to find all directory paths that are at a given depth or deeper, see the bottom section.

            Your approach cannot achieve that, because it finds directories at the given depth only, not also below.




            Your own clever wildcard-based approach should work in principle, but:



            • (a) it can be greatly streamlined.


            • (b) additional work is needed to limit output to the distinct list of those child folders whose subtrees are too deep.


            (a) Streamlining your approach:



            $Depth = 3
            $Path = '.'

            $Levels = '/*' * $Depth
            Get-ChildItem -Directory $Path/$Levels


            • As in your own approach, '/*' * $Depth dynamically creates a multi-directory-level wildcard expression (e.g., /*/* for a $Depth of 2) that can be appended to the input $Path to match only paths at that level.


            • The -Directory switch (PSv3+) limits matching to directories only.


            (b) Limiting output to the distinct set of top-level folder with too-deep subtrees:



            $Depth = 3
            $Path = '.'

            $Levels = '/*' * $Depth
            Get-ChildItem -Directory $Path/$Levels |
            ForEach-Object ($_.FullName -split '[\/]')[-$Depth] |
            Select-Object -Unique


            Note: Splitting by [/\] - that is, by either / or - makes the solution work on Unix-like platforms too (PowerShell Core); on Windows, -split '\' (by an escaped ) is sufficient.



            With your sample folder hierarchy, the above would yield:



            folder1
            folder2


            • If you want the full paths instead, append
              | Convert-Path -LiteralPath "$Path/$_" .


            • If you want directory-info objects ([System.IO.DirectoryInfo]) instead, append
              | Get-Item -LiteralPath "$Path/$_" .



            Optional reading: Getting folders up to, at, or beyond a certain depth:



            Note:



            • Even though the solutions below target folders (directories), you can include files too by simply omitting -Directory, or target files only by replacing -Directory with -File.


            • For simplicity, the commands implicitly target the current directory.



            At-a-given-depth-only logic:



            This is the same logic employed in the solution above; the following code lists folders at depth 2 only, i.e. those at the grandchild level (child directories of child directories) - note that, unlike with Get-ChildItem -Depth, depth counting starts with 1, i.e. 1 refers to child directories:



            $depth = 2 # grandchild directories only

            Get-ChildItem -Directory -Path ('*/' * $depth)


            • To output full paths, enclose the Get-ChildItem command in (...).FullName or pipe it to Select-Object -ExpandProperty FullName.


            • To output relative paths (e.g., folder1/test1/test/testsub), additional work is needed, because adding -Name will not work as expected in this case (it will output just the directory names):


            $depth = 2 # grandchild directories

            # Calculate the length of the path prefix for determining relative paths.
            # (Using the current dir ($PWD) as the reference path here.)
            $PrefixLen = (Convert-Path -LiteralPath $PWD).Length + 1

            $Levels = '/*' * $Depth
            Get-ChildItem -Directory -Path ('*/' * $depth) |
            ForEach-Object $_.FullName.Substring($PrefixLen)


            Up-to-a-given-depth logic:



            The PSv5+ -Depth parameter limits Get-ChildItem's recursion depth, i.e., it only finds items up to the specified depth, but note that it is depth 0, not 1 that represents the immediate children.

            Note that use of -Depth implies -Recurse, though you may specify the latter as well.



            For instance, to enumerate child folders and grandchild folders (2 levels) in the current directory, use:



            $depth = 2 # child and grandchild directories

            Get-ChildItem -Directory -Depth ($depth - 1)


            • To output full paths, enclose the Get-ChildItem command in (...).FullName or pipe it to Select-Object -ExpandProperty FullName.


            • To output relative paths, simply add the -Name switch to the Get-ChildItem call.


            At-a-given-depth-or-deeper logic:



            Limiting results to items at levels greater than or equal to a given depth requires a custom solution:



            $depth = 2 # grandchild directories and below

            Get-ChildItem -Directory -Name -Recurse |
            Where-Object ($_ -split '[/\]').Count -ge 2 |
            Get-Item -LiteralPath "$PWD/$_"


            If your input path isn't the (implied) current dir., substitute that path for $PWD.



            • To output full paths, replace Get-Item with Convert-Path.


            • To output relative paths, simply omit the Get-Item call.






            share|improve this answer





























              1














              The solution immediately below, which builds on your own, assumes that your intent is to find those child directories whose subtrees exceed a given depth.



              If you instead want to find all directory paths that are at a given depth or deeper, see the bottom section.

              Your approach cannot achieve that, because it finds directories at the given depth only, not also below.




              Your own clever wildcard-based approach should work in principle, but:



              • (a) it can be greatly streamlined.


              • (b) additional work is needed to limit output to the distinct list of those child folders whose subtrees are too deep.


              (a) Streamlining your approach:



              $Depth = 3
              $Path = '.'

              $Levels = '/*' * $Depth
              Get-ChildItem -Directory $Path/$Levels


              • As in your own approach, '/*' * $Depth dynamically creates a multi-directory-level wildcard expression (e.g., /*/* for a $Depth of 2) that can be appended to the input $Path to match only paths at that level.


              • The -Directory switch (PSv3+) limits matching to directories only.


              (b) Limiting output to the distinct set of top-level folder with too-deep subtrees:



              $Depth = 3
              $Path = '.'

              $Levels = '/*' * $Depth
              Get-ChildItem -Directory $Path/$Levels |
              ForEach-Object ($_.FullName -split '[\/]')[-$Depth] |
              Select-Object -Unique


              Note: Splitting by [/\] - that is, by either / or - makes the solution work on Unix-like platforms too (PowerShell Core); on Windows, -split '\' (by an escaped ) is sufficient.



              With your sample folder hierarchy, the above would yield:



              folder1
              folder2


              • If you want the full paths instead, append
                | Convert-Path -LiteralPath "$Path/$_" .


              • If you want directory-info objects ([System.IO.DirectoryInfo]) instead, append
                | Get-Item -LiteralPath "$Path/$_" .



              Optional reading: Getting folders up to, at, or beyond a certain depth:



              Note:



              • Even though the solutions below target folders (directories), you can include files too by simply omitting -Directory, or target files only by replacing -Directory with -File.


              • For simplicity, the commands implicitly target the current directory.



              At-a-given-depth-only logic:



              This is the same logic employed in the solution above; the following code lists folders at depth 2 only, i.e. those at the grandchild level (child directories of child directories) - note that, unlike with Get-ChildItem -Depth, depth counting starts with 1, i.e. 1 refers to child directories:



              $depth = 2 # grandchild directories only

              Get-ChildItem -Directory -Path ('*/' * $depth)


              • To output full paths, enclose the Get-ChildItem command in (...).FullName or pipe it to Select-Object -ExpandProperty FullName.


              • To output relative paths (e.g., folder1/test1/test/testsub), additional work is needed, because adding -Name will not work as expected in this case (it will output just the directory names):


              $depth = 2 # grandchild directories

              # Calculate the length of the path prefix for determining relative paths.
              # (Using the current dir ($PWD) as the reference path here.)
              $PrefixLen = (Convert-Path -LiteralPath $PWD).Length + 1

              $Levels = '/*' * $Depth
              Get-ChildItem -Directory -Path ('*/' * $depth) |
              ForEach-Object $_.FullName.Substring($PrefixLen)


              Up-to-a-given-depth logic:



              The PSv5+ -Depth parameter limits Get-ChildItem's recursion depth, i.e., it only finds items up to the specified depth, but note that it is depth 0, not 1 that represents the immediate children.

              Note that use of -Depth implies -Recurse, though you may specify the latter as well.



              For instance, to enumerate child folders and grandchild folders (2 levels) in the current directory, use:



              $depth = 2 # child and grandchild directories

              Get-ChildItem -Directory -Depth ($depth - 1)


              • To output full paths, enclose the Get-ChildItem command in (...).FullName or pipe it to Select-Object -ExpandProperty FullName.


              • To output relative paths, simply add the -Name switch to the Get-ChildItem call.


              At-a-given-depth-or-deeper logic:



              Limiting results to items at levels greater than or equal to a given depth requires a custom solution:



              $depth = 2 # grandchild directories and below

              Get-ChildItem -Directory -Name -Recurse |
              Where-Object ($_ -split '[/\]').Count -ge 2 |
              Get-Item -LiteralPath "$PWD/$_"


              If your input path isn't the (implied) current dir., substitute that path for $PWD.



              • To output full paths, replace Get-Item with Convert-Path.


              • To output relative paths, simply omit the Get-Item call.






              share|improve this answer



























                1












                1








                1







                The solution immediately below, which builds on your own, assumes that your intent is to find those child directories whose subtrees exceed a given depth.



                If you instead want to find all directory paths that are at a given depth or deeper, see the bottom section.

                Your approach cannot achieve that, because it finds directories at the given depth only, not also below.




                Your own clever wildcard-based approach should work in principle, but:



                • (a) it can be greatly streamlined.


                • (b) additional work is needed to limit output to the distinct list of those child folders whose subtrees are too deep.


                (a) Streamlining your approach:



                $Depth = 3
                $Path = '.'

                $Levels = '/*' * $Depth
                Get-ChildItem -Directory $Path/$Levels


                • As in your own approach, '/*' * $Depth dynamically creates a multi-directory-level wildcard expression (e.g., /*/* for a $Depth of 2) that can be appended to the input $Path to match only paths at that level.


                • The -Directory switch (PSv3+) limits matching to directories only.


                (b) Limiting output to the distinct set of top-level folder with too-deep subtrees:



                $Depth = 3
                $Path = '.'

                $Levels = '/*' * $Depth
                Get-ChildItem -Directory $Path/$Levels |
                ForEach-Object ($_.FullName -split '[\/]')[-$Depth] |
                Select-Object -Unique


                Note: Splitting by [/\] - that is, by either / or - makes the solution work on Unix-like platforms too (PowerShell Core); on Windows, -split '\' (by an escaped ) is sufficient.



                With your sample folder hierarchy, the above would yield:



                folder1
                folder2


                • If you want the full paths instead, append
                  | Convert-Path -LiteralPath "$Path/$_" .


                • If you want directory-info objects ([System.IO.DirectoryInfo]) instead, append
                  | Get-Item -LiteralPath "$Path/$_" .



                Optional reading: Getting folders up to, at, or beyond a certain depth:



                Note:



                • Even though the solutions below target folders (directories), you can include files too by simply omitting -Directory, or target files only by replacing -Directory with -File.


                • For simplicity, the commands implicitly target the current directory.



                At-a-given-depth-only logic:



                This is the same logic employed in the solution above; the following code lists folders at depth 2 only, i.e. those at the grandchild level (child directories of child directories) - note that, unlike with Get-ChildItem -Depth, depth counting starts with 1, i.e. 1 refers to child directories:



                $depth = 2 # grandchild directories only

                Get-ChildItem -Directory -Path ('*/' * $depth)


                • To output full paths, enclose the Get-ChildItem command in (...).FullName or pipe it to Select-Object -ExpandProperty FullName.


                • To output relative paths (e.g., folder1/test1/test/testsub), additional work is needed, because adding -Name will not work as expected in this case (it will output just the directory names):


                $depth = 2 # grandchild directories

                # Calculate the length of the path prefix for determining relative paths.
                # (Using the current dir ($PWD) as the reference path here.)
                $PrefixLen = (Convert-Path -LiteralPath $PWD).Length + 1

                $Levels = '/*' * $Depth
                Get-ChildItem -Directory -Path ('*/' * $depth) |
                ForEach-Object $_.FullName.Substring($PrefixLen)


                Up-to-a-given-depth logic:



                The PSv5+ -Depth parameter limits Get-ChildItem's recursion depth, i.e., it only finds items up to the specified depth, but note that it is depth 0, not 1 that represents the immediate children.

                Note that use of -Depth implies -Recurse, though you may specify the latter as well.



                For instance, to enumerate child folders and grandchild folders (2 levels) in the current directory, use:



                $depth = 2 # child and grandchild directories

                Get-ChildItem -Directory -Depth ($depth - 1)


                • To output full paths, enclose the Get-ChildItem command in (...).FullName or pipe it to Select-Object -ExpandProperty FullName.


                • To output relative paths, simply add the -Name switch to the Get-ChildItem call.


                At-a-given-depth-or-deeper logic:



                Limiting results to items at levels greater than or equal to a given depth requires a custom solution:



                $depth = 2 # grandchild directories and below

                Get-ChildItem -Directory -Name -Recurse |
                Where-Object ($_ -split '[/\]').Count -ge 2 |
                Get-Item -LiteralPath "$PWD/$_"


                If your input path isn't the (implied) current dir., substitute that path for $PWD.



                • To output full paths, replace Get-Item with Convert-Path.


                • To output relative paths, simply omit the Get-Item call.






                share|improve this answer















                The solution immediately below, which builds on your own, assumes that your intent is to find those child directories whose subtrees exceed a given depth.



                If you instead want to find all directory paths that are at a given depth or deeper, see the bottom section.

                Your approach cannot achieve that, because it finds directories at the given depth only, not also below.




                Your own clever wildcard-based approach should work in principle, but:



                • (a) it can be greatly streamlined.


                • (b) additional work is needed to limit output to the distinct list of those child folders whose subtrees are too deep.


                (a) Streamlining your approach:



                $Depth = 3
                $Path = '.'

                $Levels = '/*' * $Depth
                Get-ChildItem -Directory $Path/$Levels


                • As in your own approach, '/*' * $Depth dynamically creates a multi-directory-level wildcard expression (e.g., /*/* for a $Depth of 2) that can be appended to the input $Path to match only paths at that level.


                • The -Directory switch (PSv3+) limits matching to directories only.


                (b) Limiting output to the distinct set of top-level folder with too-deep subtrees:



                $Depth = 3
                $Path = '.'

                $Levels = '/*' * $Depth
                Get-ChildItem -Directory $Path/$Levels |
                ForEach-Object ($_.FullName -split '[\/]')[-$Depth] |
                Select-Object -Unique


                Note: Splitting by [/\] - that is, by either / or - makes the solution work on Unix-like platforms too (PowerShell Core); on Windows, -split '\' (by an escaped ) is sufficient.



                With your sample folder hierarchy, the above would yield:



                folder1
                folder2


                • If you want the full paths instead, append
                  | Convert-Path -LiteralPath "$Path/$_" .


                • If you want directory-info objects ([System.IO.DirectoryInfo]) instead, append
                  | Get-Item -LiteralPath "$Path/$_" .



                Optional reading: Getting folders up to, at, or beyond a certain depth:



                Note:



                • Even though the solutions below target folders (directories), you can include files too by simply omitting -Directory, or target files only by replacing -Directory with -File.


                • For simplicity, the commands implicitly target the current directory.



                At-a-given-depth-only logic:



                This is the same logic employed in the solution above; the following code lists folders at depth 2 only, i.e. those at the grandchild level (child directories of child directories) - note that, unlike with Get-ChildItem -Depth, depth counting starts with 1, i.e. 1 refers to child directories:



                $depth = 2 # grandchild directories only

                Get-ChildItem -Directory -Path ('*/' * $depth)


                • To output full paths, enclose the Get-ChildItem command in (...).FullName or pipe it to Select-Object -ExpandProperty FullName.


                • To output relative paths (e.g., folder1/test1/test/testsub), additional work is needed, because adding -Name will not work as expected in this case (it will output just the directory names):


                $depth = 2 # grandchild directories

                # Calculate the length of the path prefix for determining relative paths.
                # (Using the current dir ($PWD) as the reference path here.)
                $PrefixLen = (Convert-Path -LiteralPath $PWD).Length + 1

                $Levels = '/*' * $Depth
                Get-ChildItem -Directory -Path ('*/' * $depth) |
                ForEach-Object $_.FullName.Substring($PrefixLen)


                Up-to-a-given-depth logic:



                The PSv5+ -Depth parameter limits Get-ChildItem's recursion depth, i.e., it only finds items up to the specified depth, but note that it is depth 0, not 1 that represents the immediate children.

                Note that use of -Depth implies -Recurse, though you may specify the latter as well.



                For instance, to enumerate child folders and grandchild folders (2 levels) in the current directory, use:



                $depth = 2 # child and grandchild directories

                Get-ChildItem -Directory -Depth ($depth - 1)


                • To output full paths, enclose the Get-ChildItem command in (...).FullName or pipe it to Select-Object -ExpandProperty FullName.


                • To output relative paths, simply add the -Name switch to the Get-ChildItem call.


                At-a-given-depth-or-deeper logic:



                Limiting results to items at levels greater than or equal to a given depth requires a custom solution:



                $depth = 2 # grandchild directories and below

                Get-ChildItem -Directory -Name -Recurse |
                Where-Object ($_ -split '[/\]').Count -ge 2 |
                Get-Item -LiteralPath "$PWD/$_"


                If your input path isn't the (implied) current dir., substitute that path for $PWD.



                • To output full paths, replace Get-Item with Convert-Path.


                • To output relative paths, simply omit the Get-Item call.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 7 at 14:35

























                answered Mar 6 at 18:33









                mklement0mklement0

                135k22252289




                135k22252289























                    -1














                    Please try this



                    $Folders = Get-ChildItem 'C:Program Files' -Directory -Recurse -Depth 1 | 
                    Select-Object -ExpandProperty fullname |
                    Sort-Object
                    $Folders





                    share|improve this answer


















                    • 1





                      -Depth 1 will limit the recursion to the first 2 levels, so you won't know which directories have subtrees that go deeper, which is what the OP is after.

                      – mklement0
                      Mar 6 at 18:35















                    -1














                    Please try this



                    $Folders = Get-ChildItem 'C:Program Files' -Directory -Recurse -Depth 1 | 
                    Select-Object -ExpandProperty fullname |
                    Sort-Object
                    $Folders





                    share|improve this answer


















                    • 1





                      -Depth 1 will limit the recursion to the first 2 levels, so you won't know which directories have subtrees that go deeper, which is what the OP is after.

                      – mklement0
                      Mar 6 at 18:35













                    -1












                    -1








                    -1







                    Please try this



                    $Folders = Get-ChildItem 'C:Program Files' -Directory -Recurse -Depth 1 | 
                    Select-Object -ExpandProperty fullname |
                    Sort-Object
                    $Folders





                    share|improve this answer













                    Please try this



                    $Folders = Get-ChildItem 'C:Program Files' -Directory -Recurse -Depth 1 | 
                    Select-Object -ExpandProperty fullname |
                    Sort-Object
                    $Folders






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 6 at 18:11









                    thiyagu selvarajthiyagu selvaraj

                    543




                    543







                    • 1





                      -Depth 1 will limit the recursion to the first 2 levels, so you won't know which directories have subtrees that go deeper, which is what the OP is after.

                      – mklement0
                      Mar 6 at 18:35












                    • 1





                      -Depth 1 will limit the recursion to the first 2 levels, so you won't know which directories have subtrees that go deeper, which is what the OP is after.

                      – mklement0
                      Mar 6 at 18:35







                    1




                    1





                    -Depth 1 will limit the recursion to the first 2 levels, so you won't know which directories have subtrees that go deeper, which is what the OP is after.

                    – mklement0
                    Mar 6 at 18:35





                    -Depth 1 will limit the recursion to the first 2 levels, so you won't know which directories have subtrees that go deeper, which is what the OP is after.

                    – mklement0
                    Mar 6 at 18:35

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55029472%2flist-folders-at-or-below-a-given-depth-in-powershell%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    1928 у кіно

                    Захаров Федір Захарович

                    Ель Греко