我正在使用PowerShell 2.0,我想將所有特定路徑的子目錄導出。以下命令輸出所有文件和目錄,但我無法弄清楚如何過濾掉這些文件。如何僅使用Get-ChildItem獲取目錄?
Get-ChildItem c:\mypath -Recurse
我用$_.Attributes
得到的屬性嘗試,但當時我不知道如何構建的System.IO.FileAttributes
文字實例來比較它。在cmd.exe
這將是
dir /b /ad /s
我正在使用PowerShell 2.0,我想將所有特定路徑的子目錄導出。以下命令輸出所有文件和目錄,但我無法弄清楚如何過濾掉這些文件。如何僅使用Get-ChildItem獲取目錄?
Get-ChildItem c:\mypath -Recurse
我用$_.Attributes
得到的屬性嘗試,但當時我不知道如何構建的System.IO.FileAttributes
文字實例來比較它。在cmd.exe
這將是
dir /b /ad /s
對於PowerShell的版本低於3.0:
通過Get-ChildItem
返回的FileInfo
對象有一個 「基地」 屬性,PSIsContainer
。您只想選擇這些項目。
Get-ChildItem -Recurse | ?{ $_.PSIsContainer }
如果你想要的目錄的原始字符串名稱,你可以做
Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | Select-Object FullName
對於PowerShell的3.0和更高版本:
dir -Directory
用途:
dir -r | where { $_ -is [System.IO.DirectoryInfo] }
更清潔的方法:
Get-ChildItem "<name_of_directory>" | where {$_.Attributes -match'Directory'}
我不知道PowerShell 3.0是否有一個交換機只返回目錄;這似乎是一個合乎邏輯的事情補充。
它很好但不幸失敗(例如)只讀目錄,因爲屬性轉換爲字符串「ReadOnly,Directory」。有沒有辦法直接匹配FileAttributes枚舉的Directory元素? – 2012-09-07 09:30:18
粗體回答:Get-ChildItem「
FYI powershell 3.0增加了'-Directory'和'-File'標誌 – WickyNilliams 2013-04-17 14:12:08
在PowerShell中3.0,它是簡單的:
Get-ChildItem -Directory #List only directories
Get-ChildItem -File #List only files
dir是Get-ChildItem的別名 – 2013-06-21 20:02:43
@crashmstr你確定嗎?我檢查了我的PS4.0。對我而言,'dir'被別名爲'Get-ChildItem',而'-Directory'和'-File'選項如上所述。我使用命令'echo $ PSVersionTable','help dir','dir -Directory'和'dir -File'來提出這個評論。 – 2015-03-11 10:29:17
這應該是答案 – 2015-10-02 13:45:50
較少的文本是需要用這個辦法:
ls -r | where {$_.mode -match "d"}
爲什麼這是-1'd? – 2013-04-23 17:33:44
這是我會用的。 – 2014-01-09 17:56:45
更短:ls -r | ? {$ _。mode -match「d」} – jyggorath 2014-05-21 11:31:04
使用這一個:
Get-ChildItem -Path \\server\share\folder\ -Recurse -Force | where {$_.Attributes -like '*Directory*'} | Export-Csv -Path C:\Temp\Export.csv -Encoding "Unicode" -Delimiter ";"
有點更具可讀性和簡單的方法可以通過以下腳本實現:
$Directory = "./"
Get-ChildItem $Directory -Recurse | % {
if ($_.Attributes -eq "Directory") {
Write-Host $_.FullName
}
}
希望這有助於!
從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"
用途:
dir -Directory -Recurse | Select FullName
這會給你的根結構的輸出和僅用於目錄中的文件夾名稱。
接受的答案提到
Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | Select-Object FullName
得到一個 「原始字符串」。 但實際上,Selected.System.IO.DirectoryInfo
類型的對象將被退回。對於原始字符串可以使用如下:
Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | % { $_.FullName }
如果該值串聯爲一個字符串的區別很重要:
Select-Object
令人驚訝foo\@{FullName=bar}
ForEach
- 運算符預期:foo\bar
Select-Object實際上會返回PSCustomObject類型的對象。雖然你可以使用%(這是ForEach對象)來獲取原始字符串,但你也可以使用'Select-Object -ExpandProperty FullName' – JamesQMurphy 2015-02-09 22:20:50
用途:
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
convertto-csv -NoTypeInformation
Out-File c:\temp\mydirectorylist.csv
如果你可以解釋每一步以便更多人可以看到到底是怎麼回事。 – jazzurro 2014-10-20 02:43:23
這不適用於OP的特定需求的Powershell 2.0。 Powershell 2.0中的 - [/ Dir/Directory]不是有效的參數 – 2017-08-02 20:44:21
我的解決方案基於TechNet文章Fun Things You Can Do With the Get-ChildItem Cmdlet。
Get-ChildItem C:\foo | Where-Object {$_.mode -match "d"}
我用它在我的腳本中,它運作良好。
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
遞歸子目錄以及加012選項。
ls -dir -r #lists only directories recursively
ls -file -r #lists only files recursively
在PowerShell 4.0和PowerShell 5.0(Windows 10)上測試。
這不適用於OP的特定需求的Powershell 2.0。 - [/ Dir/Directory]在Powershell 2.0中不是有效的參數 – 2017-08-02 20:41:55
你會想要使用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 ;
}
爲了回答專門(使用IO.FileAttributes)原題:
Get-ChildItem c:\mypath -Recurse | Where-Object {$_.Attributes -and [IO.FileAttributes]::Directory}
我雖然喜歡馬立克氏溶液(Where-Object { $_ -is [System.IO.DirectoryInfo] }
)。
希望被別名爲「IsFolder」。 – xcud 2010-06-21 14:41:30
完美,謝謝。我知道必須有一個簡單的方法! – 2010-06-21 14:46:28
xcud:並非每個由PSDrive代表的層級都是基於文件夾的。 – Joey 2010-06-21 19:36:42