我正在使用 PowerShell 2.0,我想通过管道输出某个路径的所有子目录。以下命令输出所有文件和目录,但我不知道如何过滤掉这些文件。
Get-ChildItem c:\mypath -Recurse
我尝试使用 $_.Attributes
来获取属性,但后来我不知道如何构造 System.IO.FileAttributes
的文字实例来比较它。在 cmd.exe
中它将是
dir /b /ad /s
对于 PowerShell 3.0 及更高版本:
Get-ChildItem -Directory
您还可以使用别名 dir
、ls
和 gci
对于低于 3.0 的 PowerShell 版本:
Get-ChildItem
返回的 FileInfo
对象具有“基本”属性 PSIsContainer
。您只想选择那些项目。
Get-ChildItem -Recurse | ?{ $_.PSIsContainer }
如果你想要目录的原始字符串名称,你可以做
Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | Select-Object FullName
在 PowerShell 3.0 中,它更简单:
Get-ChildItem -Directory #List only directories
Get-ChildItem -File #List only files
dir
是 Get-ChildItem
的别名,-Directory
和 -File
选项按描述工作。我使用命令 echo $PSVersionTable
、help dir
、dir -Directory
和 dir -File
来提出这条评论。
ls
和 dir
是 Get-ChildItem
的别名选择你的毒药
利用
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
要也递归子目录,请添加 -r
选项。
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
开关时不遵循符号链接。要遵循符号链接,请使用 -r
指定 -FollowSymlink
开关。
注意 2:PowerShell 现在是跨平台的,从 6.0 版开始。跨平台版本最初称为 PowerShell Core,但自 PowerShell 7.0+ 起,“Core”一词已被删除。
Get-ChildItem 文档:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem
更清洁的方法:
Get-ChildItem "<name_of_directory>" | where {$_.Attributes -match'Directory'}
我想知道 PowerShell 3.0 是否有一个只返回目录的开关;添加似乎是合乎逻辑的事情。
-Directory
和 -File
标志
利用:
dir -r | where { $_ -is [System.IO.DirectoryInfo] }
从 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"
这种方法需要更少的文本:
ls -r | ? {$_.mode -match "d"}
接受的答案提到
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
Select-Object -ExpandProperty FullName
利用:
dir -Directory -Recurse | Select FullName
这将为您提供根结构的输出,其中仅包含目录的文件夹名称。
您需要首先使用 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 ;
}
利用:
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
使用下面的脚本可以实现更易读和更简单的方法:
$Directory = "./"
Get-ChildItem $Directory -Recurse | % {
if ($_.Attributes -eq "Directory") {
Write-Host $_.FullName
}
}
希望这可以帮助!
我的解决方案基于 TechNet 文章 Fun Things You Can Do With the Get-ChildItem Cmdlet。
Get-ChildItem C:\foo | Where-Object {$_.mode -match "d"}
我在我的脚本中使用了它,并且效果很好。
这个问题得到了很好的回答,但我想我会添加一些额外的东西,因为我一直在看这个。
Get-ChildItem
恰好产生 两种 类型的对象,而大多数命令只产生一种。
返回 FileInfo 和 DirectoryInfo。
您可以通过查看此命令可用的“成员”来查看这一点,如下所示:
Get-ChildItem | Get-Member
类型名称:System.IO.DirectoryInfo
类型名称:System.IO.FileInfo
您将看到可用于每种类型的各种方法和属性。请注意,存在差异。例如,FileInfo 对象具有长度属性,但 DirectoryInfo 对象没有。
无论如何,从技术上讲,我们可以通过隔离 DirectoryInfo 对象只返回目录
Get-ChildItem | Where-Object {$_.GetType().Name -eq "DirectoryInfo"}
显然,正如最佳答案所述,最直接的解决方案是简单地使用 Get-ChildItem -Directory
但我们现在知道将来如何使用多种对象类型:)
使用这个:
Get-ChildItem -Path \\server\share\folder\ -Recurse -Force | where {$_.Attributes -like '*Directory*'} | Export-Csv -Path C:\Temp\Export.csv -Encoding "Unicode" -Delimiter ";"
要专门回答原始问题(使用 IO.FileAttributes):
Get-ChildItem c:\mypath -Recurse | Where-Object {$_.Attributes -and [IO.FileAttributes]::Directory}
不过,我确实更喜欢 Marek 的解决方案(Where-Object { $_ -is [System.IO.DirectoryInfo] }
)。
不定期副业成功案例分享