我如何获得只有使用Get-ChildItem的目录?
我正在使用PowerShell 2.0,我想把所有的path的子目录。 以下命令输出所有的文件和目录,但我不知道如何过滤掉这些文件。
Get-ChildItem c:\mypath -Recurse  我已经尝试使用$_.Attributes来获取属性,但是我不知道如何构build一个System.IO.FileAttributes的文字实例来比较它。 在cmd.exe它会 
 dir /b /ad /s 
	
 由Get-ChildItem返回的FileInfo对象具有“基本”属性PSIsContainer 。 你只想select那些项目。 
 Get-ChildItem -Recurse | ?{ $_.PSIsContainer } 
如果你想要目录的原始string名称,你可以做
 Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | Select-Object FullName 
在PowerShell 3.0中,它更简单:
 dir -Directory #List only directories dir -File #List only files 
 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 
 为了recursion子目录,添加-r选项。 
 ls -dir -r #lists only directories recursively ls -file -r #lists only files recursively 
testingPowerShell 4.0和PowerShell 5.0(Windows 10)。
更清洁的方法:
 Get-ChildItem "<name_of_directory>" | where {$_.Attributes -match'Directory'} 
我不知道PowerShell 3.0是否有一个只返回目录的交换机; 这似乎是一个合乎逻辑的事情补充。
使用:
 dir -r | where { $_ -is [System.IO.DirectoryInfo] } 
从PowerShell v2和更新版本(k表示您开始search的文件夹):
 Get-ChildItem $Path -attributes D -Recurse 
如果你只想要文件夹名称,而没有别的,使用这个:
 Get-ChildItem $Path -Name -attributes D -Recurse 
 如果你正在寻找一个特定的文件夹,你可以使用以下。 在这种情况下,我正在寻找名为myFolder的文件夹: 
 Get-ChildItem $Path -attributes D -Recurse -include "myFolder" 
这种方法需要的文字较less:
 ls -r | where {$_.mode -match "d"} 
使用:
 dir -Directory -Recurse | Select 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 } } 
希望这可以帮助!
接受的答案提到
 Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | Select-Object FullName 
 得到一个“原始string”。 但事实上, Selected.System.IO.DirectoryInfotypes的对象将被返回。 对于原始string,可以使用以下内容: 
 Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | % { $_.FullName } 
如果将值连接到一个string,则区别很重要:
-  与Select-Object令人惊讶的foo\@{FullName=bar}
-  与ForEach运营商的预期:foo\bar
我的解决scheme基于TechNet文章, 您可以使用Get-ChildItem Cmdlet执行有趣的事情 。
 Get-ChildItem C:\foo | Where-Object {$_.mode -match "d"} 
我用它在我的脚本,它运作良好。
使用这个:
 Get-ChildItem -Path \\server\share\folder\ -Recurse -Force | where {$_.Attributes -like '*Directory*'} | Export-Csv -Path C:\Temp\Export.csv -Encoding "Unicode" -Delimiter ";" 
您将要使用Get-ChildItemrecursion获取所有文件夹和文件。 然后将该输出传送到只接收这些文件的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 ; } 
要专门回答原始问题(使用IO.FileAttributes):
 Get-ChildItem c:\mypath -Recurse | Where-Object {$_.Attributes -and [IO.FileAttributes]::Directory} 
 我喜欢马立克的解决scheme( Where-Object { $_ -is [System.IO.DirectoryInfo] } )。