How do I get only directories using Get-ChildItem?

后端 未结 15 1385
北海茫月
北海茫月 2020-11-29 16:30

I\'m using PowerShell 2.0 and I want to pipe out all the subdirectories of a certain path. The following command outputs all files and directories, but I can\'t figure out h

15条回答
  •  萌比男神i
    2020-11-29 16:40

    For PowerShell versions less than 3.0:

    The FileInfo object returned by Get-ChildItem has a "base" property, PSIsContainer. You want to select only those items.

    Get-ChildItem -Recurse | ?{ $_.PSIsContainer }
    

    If you want the raw string names of the directories, you can do

    Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | Select-Object FullName
    

    For PowerShell 3.0 and greater:

    Get-ChildItem -Directory
    

    You can also use the aliases dir, ls, and gci

提交回复
热议问题