如何使用Get-ChildItem仅获取目录?


242

我正在使用PowerShell 2.0,并且希望输出特定路径的所有子目录。以下命令输出所有文件和目录,但是我不知道如何过滤掉文件。

Get-ChildItem c:\mypath -Recurse

我尝试使用$_.Attributes获取属性,但是后来我不知道如何构造一个System.IO.FileAttributes与之比较的文字实例。在cmd.exe其中

dir /b /ad /s

Answers:


292

对于低于3.0的PowerShell版本:

FileInfo返回的对象Get-ChildItem具有“基本”属性PSIsContainer。您只想选择那些项目。

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

如果您想要目录的原始字符串名称,则可以执行

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

对于PowerShell 3.0及更高版本:

dir -Directory

20
希望别名为“ IsFolder”。
xcud 2010年

5
xcud:并非PSDrive代表的每个层次结构都基于文件夹。
乔伊(Joey)

10
“容器”和“文件夹”之间的语义差距不是您可以驾驶卡车穿越的。
xcud 2010年

5
@xcud:请参阅iraSenthil的答案。-Directory和-File也可以在Get-ChildItem上使用。无需直接使用PSIsContainer属性。
Wouter

(Get-ChildItem |?{$ _。PSIsContainer} |选择名称)。名称
Jose Ortega

195

在PowerShell 3.0中,它更简单:

Get-ChildItem -Directory #List only directories
Get-ChildItem -File #List only files

33
dir是Get-ChildItem的别名
Chip McCormick

1
@crashmstr您确定吗?我检查了我的PS4.0。对我而言,dir别名为Get-ChildItem,并且-Directory-File选项按所述方式工作。我使用的命令echo $PSVersionTablehelp dirdir -Directorydir -File想出这条评论。
彼得·赫尔

3
这应该是答案
Chris S

2
lsdirGet-ChildItem挑毒的别名
Kolob Canyon'Rob

53

Get-ChildItem -dir #lists only directories
Get-ChildItem -file #lists only files

如果您喜欢别名,请使用

ls -dir #lists only directories
ls -file #lists only files

要么

dir -dir #lists only directories
dir -file #lists only files

要也递归子目录,请添加-roption。

ls -dir -r #lists only directories recursively
ls -file -r #lists only files recursively 

在PowerShell 4.0,PowerShell 5.0(Windows 10),PowerShell Core 6.0(Windows 10,Mac和Linux)和PowerShell 7.0(Windows 10,Mac和Linux)上进行了测试。

注意:在PowerShell Core上,指定-r开关时不遵循符号链接。要遵循符号链接,请使用指定-FollowSymlink开关-r

注意2:从6.0版开始,PowerShell现在是跨平台的。跨平台版本最初称为PowerShell Core,但自PowerShell 7.0+起已删除单词“ Core”。

Get-ChildItem文档:https : //docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.management/get-childitem


2
这在OP的特定需求中无法在Powershell 2.0中起作用。-[/ Dir / Directory]在Powershell 2.0中不是有效的参数
Ben Personick

21

一种更清洁的方法:

Get-ChildItem "<name_of_directory>" | where {$_.Attributes -match'Directory'}

我想知道PowerShell 3.0是否具有仅返回目录的开关;添加似乎是合乎逻辑的事情。


1
FYI powershell 3.0添加-Directory-File标志
WickyNilliams 2013年


6

在PowerShell v2及更高版本中(k表示您要在其中开始搜索的文件夹):

Get-ChildItem $Path -attributes D -Recurse

如果您只想要文件夹名称,而别无其他,请使用以下命令:

Get-ChildItem $Path -Name -attributes D -Recurse

如果要查找特定的文件夹,则可以使用以下内容。在这种情况下,我正在寻找一个名为的文件夹myFolder

Get-ChildItem $Path -attributes D -Recurse -include "myFolder"

并使用PS 3.0 o 4.0?
Kiquenet

5
似乎PS2中没有Attributes参数,它给出了一个错误“找不到与参数名称'Attributes'相匹配的参数”。在PS3中可以正常工作。
WileCau 2014年

5

这种方法所需的文字更少:

ls -r | ? {$_.mode -match "d"}

这是我要使用的那个。
David Betz 2014年

2
甚至更短:ls -r | ?{$ _。mode -match“ d”}
jyggorath 2014年

找不到压缩的文件夹
Charles Lambert

7
因为压缩文件夹是zip文件
所以user1594322

递归所有内容并不总是有用的。您可能拥有一棵非常深且茂密的树,您只对正好两级向下的目录感兴趣。向下搜索三十级是浪费时间。
Ross Presser

4

用:

dir -Directory -Recurse | Select FullName

这将为您提供根结构的输出,其中仅包含目录的文件夹名称。


3

用:

Get-ChildItem \\myserver\myshare\myshare\ -Directory | Select-Object -Property name |  convertto-csv -NoTypeInformation  | Out-File c:\temp\mydirectorylist.csv

以下哪个

  • 获取目标位置中的目录列表: Get-ChildItem \\myserver\myshare\myshare\ -Directory
  • 仅提取目录名称: Select-Object -Property name
  • 将输出转换为CSV格式: convertto-csv -NoTypeInformation
  • 将结果保存到文件中: Out-File c:\temp\mydirectorylist.csv

2
如果您可以解释每个步骤,以便让更多的人可以看到正在发生的事情,那就太好了。
jazzurro 2014年

这在OP的特定需求中无法在Powershell 2.0中起作用。-[/ Dir / Directory]在Powershell 2.0中不是有效的参数
Ben Personick

2

使用以下脚本可以实现更具可读性和简单性的方法:

$Directory = "./"
Get-ChildItem $Directory -Recurse | % {
    if ($_.Attributes -eq "Directory") {
        Write-Host $_.FullName
    }
}

希望这可以帮助!


2

您将要使用Get-ChildItem递归获取所有文件夹和文件。然后将输出通过管道传递到仅接收文件的Where-Object子句中。

# one of several ways to identify a file is using GetType() which
# will return "FileInfo" or "DirectoryInfo"
$files = Get-ChildItem E:\ -Recurse | Where-Object {$_.GetType().Name -eq "FileInfo"} ;

foreach ($file in $files) {
  echo $file.FullName ;
}

1

接受的答案提到

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

得到一个“原始字符串”。但实际上,Selected.System.IO.DirectoryInfo将返回类型的对象。对于原始字符串,可以使用以下内容:

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

如果值连接到字符串,则区别很重要:

  • Select-Object令人惊讶foo\@{FullName=bar}
  • 使用ForEach-operator预期:foo\bar

1
Select-Object实际上将返回PSCustomObject类型的对象。尽管您可以像使用%一样使用%(它是ForEach-Object)来获取原始字符串,但是您也可以使用Select-Object -ExpandProperty FullName
JamesQMurphy 2015年


0

使用这个:

Get-ChildItem -Path \\server\share\folder\ -Recurse -Force | where {$_.Attributes -like '*Directory*'} | Export-Csv -Path C:\Temp\Export.csv -Encoding "Unicode" -Delimiter ";"

0

要专门回答原始问题(使用IO.FileAttributes):

Get-ChildItem c:\mypath -Recurse | Where-Object {$_.Attributes -and [IO.FileAttributes]::Directory}

我确实更喜欢Marek的解决方案(Where-Object { $_ -is [System.IO.DirectoryInfo] })。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.