2010-06-21 27 views
161

我正在使用PowerShell 2.0,我想將所有特定路徑的子目錄導出。以下命令輸出所有文件和目錄,但我無法弄清楚如何過濾掉這些文件。如何僅使用Get-ChildItem獲取目錄?

Get-ChildItem c:\mypath -Recurse 

我用$_.Attributes得到的屬性嘗試,但當時我不知道如何構建的System.IO.FileAttributes文字實例來比較它。在cmd.exe這將是

dir /b /ad /s 

回答

195

對於PowerShell的版本低於3.0:

通過Get-ChildItem返回的FileInfo對象有一個 「基地」 屬性,PSIsContainer。您只想選擇這些項目。

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

如果你想要的目錄的原始字符串名稱,你可以做

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

對於PowerShell的3.0和更高版本:

dir -Directory 
+13

希望被別名爲「IsFolder」。 – xcud 2010-06-21 14:41:30

+1

完美,謝謝。我知道必須有一個簡單的方法! – 2010-06-21 14:46:28

+5

xcud:並非每個由PSDrive代表的層級都是基於文件夾的。 – Joey 2010-06-21 19:36:42

11

用途:

dir -r | where { $_ -is [System.IO.DirectoryInfo] } 
18

更清潔的方法:

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

我不知道PowerShell 3.0是否有一個交換機只返回目錄;這似乎是一個合乎邏輯的事情補充。

+1

它很好但不幸失敗(例如)只讀目錄,因爲屬性轉換爲字符串「ReadOnly,Directory」。有沒有辦法直接匹配FileAttributes枚舉的Directory元素? – 2012-09-07 09:30:18

+5

粗體回答:Get-ChildItem「」|其中{$ _。Attributes -match'Directory'} – 2012-09-07 13:04:56

+1

FYI powershell 3.0增加了'-Directory'和'-File'標誌 – WickyNilliams 2013-04-17 14:12:08

136

在PowerShell中3.0,它是簡單的:

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

dir是Get-ChildItem的別名 – 2013-06-21 20:02:43

+1

@crashmstr你確定嗎?我檢查了我的PS4.0。對我而言,'dir'被別名爲'Get-ChildItem',而'-Directory'和'-File'選項如上所述。我使用命令'echo $ PSVersionTable','help dir','dir -Directory'和'dir -File'來提出這個評論。 – 2015-03-11 10:29:17

+3

這應該是答案 – 2015-10-02 13:45:50

4

較少的文本是需要用這個辦法:

ls -r | where {$_.mode -match "d"} 
+0

爲什麼這是-1'd? – 2013-04-23 17:33:44

+0

這是我會用的。 – 2014-01-09 17:56:45

+1

更短:ls -r | ? {$ _。mode -match「d」} – jyggorath 2014-05-21 11:31:04

0

使用這一個:

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

有點更具可讀性和簡單的方法可以通過以下腳本實現:

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

希望這有助於!

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" 
+0

並使用PS 3.0 o 4.0? – Kiquenet 2013-12-27 09:36:14

+3

Attributes參數似乎不在PS2中,它給出了一個錯誤「找不到與參數名'屬性'匹配的參數」。它在PS3中正常工作。 – WileCau 2014-05-26 07:02:37

3

用途:

dir -Directory -Recurse | Select FullName 

這會給你的根結構的輸出和僅用於目錄中的文件夾名稱。

1

接受的答案提到

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

得到一個 「原始字符串」。 但實際上,Selected.System.IO.DirectoryInfo類型的對象將被退回。對於原始字符串可以使用如下:

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

如果該值串聯爲一個字符串的區別很重要:

  • Select-Object令人驚訝foo\@{FullName=bar}
  • ForEach - 運算符預期:foo\bar
+0

Select-Object實際上會返回PSCustomObject類型的對象。雖然你可以使用%(這是ForEach對象)來獲取原始字符串,但你也可以使用'Select-Object -ExpandProperty FullName' – JamesQMurphy 2015-02-09 22:20:50

2

用途:

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-10-20 02:43:23

+0

這不適用於OP的特定需求的Powershell 2.0。 Powershell 2.0中的 - [/ Dir/Directory]不是有效的參數 – 2017-08-02 20:44:21

39
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)上測試。

+1

這不適用於OP的特定需求的Powershell 2.0。 - [/ Dir/Directory]在Powershell 2.0中不是有效的參數 – 2017-08-02 20:41:55

0

你會想要使用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 ; 
} 
0

爲了回答專門(使用IO.FileAttributes)原題:

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

我雖然喜歡馬立克氏溶液(Where-Object { $_ -is [System.IO.DirectoryInfo] })。