none
Script to list and extract folders and subfolders names RRS feed

  • Question

  • Hello all

    I need a script IN POWERSHELL to list and extract folders and subfolders names.

    I have already tried Get-ChildItem with its options but could not realize how to extract the directories names.

    Thanks,

    De Lucca


    Monday, February 5, 2018 1:27 PM

Answers

  • Try this to list all folder and sub-folder. hope this would work for you.

    Get-ChildItem -path C:\test -Directory -recurse | Select-Object -Property name | convertto-csv -NoTypeInformation  | Out-File c:\temp\mydirectorylist.csv

    • Marked as answer by Rafa de Lucca Tuesday, February 6, 2018 4:23 PM
    Tuesday, February 6, 2018 10:29 AM
  • Thank you!

    I could use

    Get-ChildItem c:\any\path\*\*\* | Select-Object FullName

    \* for each subdirectory

    • Marked as answer by Rafa de Lucca Tuesday, February 6, 2018 4:25 PM
    Tuesday, February 6, 2018 4:25 PM

All replies

  • Get-Childitem -Path #PATH -Directory -Recurse
    Should give you all directory names under the path
    Monday, February 5, 2018 1:39 PM
  • If you only want the names:

    (Get-Childitem -Path "c:\my\path" -Directory -Recurse).name

    Monday, February 5, 2018 1:47 PM
  • Great! Thank you!

    And how to extract subdirectories names up to a limit of 7 subdirectories (all subdirectories 7 levels deeper)?

    Monday, February 5, 2018 2:33 PM
  • (Get-Childitem -Path "c:\my\path" -Directory -Recurse -Depth 7).name

    should work
    Monday, February 5, 2018 2:48 PM
  • k!
    Do you have any other idea?

    I had already tried something like that before and -Depth is not an option.

    PS C:\any\path> (Get-Childitem -Path "c:\any\path" -Directory -Recurse -Depth 7).name
    Get-ChildItem : A parameter cannot be found that matches parameter name 'Depth'.
    At line:1 char:50
    + (Get-Childitem -Path "c:\any\path" -Directory -Recurse -Depth 7).name
    +                                                  ~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
        + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

    Thank you anyway


    ** path name edited
    Monday, February 5, 2018 2:56 PM
  • Check your version of PowerShell and make sure you're up to date. Use:

    $PSVersionTable.PSVersion

    In a PowerShell prompt to find out the version you are running.

    Monday, February 5, 2018 3:10 PM
  • Hello!

    Any other command on Powershell we could use to list the complete path name of folders?

    by the way...
    PS C:\hp> $PSVersionTable.PSVersion

    Major  Minor  Build  Revision
    -----  -----  -----  --------
    4      0      -1     -1     

    Monday, February 5, 2018 6:05 PM
  • Update to PowerShell version 5 if possible: https://www.microsoft.com/en-us/download/details.aspx?id=50395

    "ls" is a Unix command but works in PowerShell. Would strongly recommend upgrading as I'm not sure how well "ls" works in the long run.


    Tuesday, February 6, 2018 9:23 AM
  • help ls

    Note what the command is.  It is not a Linux command.


    \_(ツ)_/

    Tuesday, February 6, 2018 9:45 AM
  • Holy crap, just blew my mind a little. Had no idea that "ls" can be used instead of "Get-ChildItem" and still uses all the parameters. Thank you, oh great one :)
    Tuesday, February 6, 2018 10:04 AM
  • "dir" would do the same
    Tuesday, February 6, 2018 10:08 AM
  • That is why we constantly recommend that GUI techs spend time learning PowerSHell form a book or tutorial.  This is all PS 101:

    Get-Alias

    Also "dir"

    Most newbies have not even learned to use help:

    Start with: help help

    and rad all that you can do with "help".


    \_(ツ)_/

    Tuesday, February 6, 2018 10:10 AM
  • Don't forget the first rule taught to all Unix users ... "man man"

    Type that in PS and see what you get.


    \_(ツ)_/

    Tuesday, February 6, 2018 10:11 AM
  • Try this to list all folder and sub-folder. hope this would work for you.

    Get-ChildItem -path C:\test -Directory -recurse | Select-Object -Property name | convertto-csv -NoTypeInformation  | Out-File c:\temp\mydirectorylist.csv

    • Marked as answer by Rafa de Lucca Tuesday, February 6, 2018 4:23 PM
    Tuesday, February 6, 2018 10:29 AM
  • Try this to list all folder and sub-folder. hope this would work for you.

    Get-ChildItem -path C:\test -Directory -recurse | Select-Object -Property name | convertto-csv -NoTypeInformation  | Out-File c:\temp\mydirectorylist.csv

    Why are you using Convert?

    Get-ChildItem -path C:\test -Directory -recurse | Select FullName | Export-csv <file path name> -NoType

    Again --- PS 101


    \_(ツ)_/


    • Edited by jrv Tuesday, February 6, 2018 10:46 AM
    Tuesday, February 6, 2018 10:33 AM
  • following is the command which is working for me to what level you set to get that 2 nested level

    Get-ChildItem -path 'C:\test\' -Directory -depth 2 | Select-Object -Property name

    for 7, set -depth parameter to 7, and its working in same way

    Get-ChildItem -path 'C:\test\' -Directory -depth 7 | Select-Object -Property name

    Tuesday, February 6, 2018 10:41 AM
  • Just to have output converted in csv format
    Tuesday, February 6, 2018 10:44 AM
  • As noted above ... PowerShell 4 (WMF 4) does not have "Depth" on that command.  It is only available on PS 5 and later.

    ConvertTo-Csv is for convenience when you need to edit the CSV as text before saving.


    \_(ツ)_/


    • Edited by jrv Tuesday, February 6, 2018 10:47 AM
    Tuesday, February 6, 2018 10:45 AM
  • Just to have output converted in csv format

    That is what Export-Csv is for.

    help Export-Csv -full


    \_(ツ)_/

    Tuesday, February 6, 2018 10:46 AM
  • Thank you!

    I could use

    Get-ChildItem c:\any\path\*\*\* | Select-Object FullName

    \* for each subdirectory

    • Marked as answer by Rafa de Lucca Tuesday, February 6, 2018 4:25 PM
    Tuesday, February 6, 2018 4:25 PM
  • yes, you are correct that we can use export-csv instead of (convertto-csv | out-file), will be same result

    Wednesday, February 7, 2018 5:14 AM