適用於 FileSystem 的 Get-ChildItem

適用於: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

取得檔案系統磁碟機中的檔案和資料夾。

語法

Get-ChildItem [-Attributes <FileAttributes]>] [-Directory] [-File] [-Force] [-Hidden] [-ReadOnly] [-System] [-UseTransaction] [<CommonParameters>]

說明

在檔案系統磁碟機中,Get-ChildItem Cmdlet 會取得目錄、子目錄和檔案。在檔案系統目錄中,它會取得子目錄和檔案。

根據預設值,Get-ChildItem 會取得非隱藏的項目,但您可以使用 Directory、File、Hidden、ReadOnly 及 System 參數,只取得含有這些屬性的項目。若要建立複雜的屬性搜尋,請使用 Attributes 參數。如果您使用這些參數,Get-ChildItem 就只會取得符合所有搜尋條件的項目,如同透過 AND 運算子來連接這些參數。

注意:這個自訂的 Cmdlet 說明檔說明 Get-ChildItem Cmdlet 如何在檔案系統磁碟機中運作。如需 Get-ChildItem Cmdlet 在所有磁碟機中的相關資訊,請輸入 "Get-HelpGet-ChildItem -Path $null",或請參閱 Get-ChildItem,網址為 https://go.microsoft.com/fwlink/?LinkID=113308。

參數

-Attributes <FileAttributes]>

取得具有指定屬性的檔案和資料夾。此參數支援所有屬性,可讓您指定複雜的屬性組合。

例如,若要取得已加密或壓縮的非系統檔案 (非目錄),請輸入:

Get-ChildItem -Attributes !Directory+!System+Encrypted, !Directory+!System+Compressed

若要使用常用屬性來尋找檔案與資料夾,您可以使用 Attributes 參數,或是 Directory、File、Hidden、ReadOnly 及 System 切換參數。

Attributes 參數支援下列屬性:Archive、Compressed、Device、Directory、Encrypted、Hidden、Normal、NotContentIndexed、Offline、ReadOnly、ReparsePoint、SparseFile、System 及 Temporary。如需這些屬性的描述,請參閱<FileAttributes 列舉>,網址為 https://go.microsoft.com/fwlink/?LinkId=201508。

使用下列運算子來結合屬性。

! NOT

+ AND

, OR

運算子和其屬性之間不允許有任何空格。不過,逗號之前可以有空格。

您可以針對常用屬性使用下列縮寫:

D Directory

H Hidden

R Read-only

S System

必要?

false

位置?

具名

預設值

接受管線輸入?

false

接受萬用字元?

false

-Directory

取得目錄 (資料夾)。

若只要取得目錄,請使用 Directory 參數並省略 File 參數。若要排除目錄,請使用 File 參數並省略 Directory 參數,或使用 Attributes 參數。

若要取得目錄,請使用 Directory 參數、其 "ad" 別名或 Attributes 參數的 Directory 屬性。

必要?

false

位置?

具名

預設值

接受管線輸入?

false

接受萬用字元?

false

-File

取得檔案。

若只要取得檔案,請使用 File 參數並省略 Directory 參數。若要排除檔案,請使用 Directory 參數並省略 File 參數,或使用 Attributes 參數。

若要取得檔案,請使用 File 參數、其 "af" 別名或 Attributes 參數的 File 值。

必要?

false

位置?

具名

預設值

接受管線輸入?

false

接受萬用字元?

false

-Hidden

只取得隱藏的檔案和目錄 (資料夾)。根據預設值,Get-ChildItem 只會取得非隱藏的項目,但是您可以使用 Force 參數,在結果中包含隱藏的項目。

若只要取得隱藏的項目,請使用 Hidden 參數、它的 "h" 或 "ah" 別名,或 Attributes 參數的 Hidden 值。若要排除隱藏的項目,請省略 Hidden 參數或使用 Attributes 參數。

必要?

false

位置?

具名

預設值

接受管線輸入?

false

接受萬用字元?

false

-ReadOnly

只取得唯讀的檔案和目錄 (資料夾)。

若只要取得唯讀項目,請使用 ReadOnly 參數、它的 "ar" 別名,或 Attributes 參數的 ReadOnly 值。若要排除唯讀項目,請使用 Attributes 參數。

必要?

false

位置?

具名

預設值

接受管線輸入?

false

接受萬用字元?

false

-System

只取得系統檔案和目錄 (資料夾)。

若只要取得系統檔案和資料夾,請使用 System 參數、它的 "as" 別名,或 Attributes 參數的 System 值。若要排除系統檔案和資料夾,請使用 Attributes 參數。

必要?

false

位置?

具名

預設值

接受管線輸入?

false

接受萬用字元?

false

-Force

取得隱藏的檔案和資料夾。根據預設值,會排除隱藏的檔案和資料夾。您也可以使用 Hidden 參數或 Attributes 參數的 Hidden 值,來取得隱藏的檔案和資料夾。

必要?

false

位置?

具名

預設值

接受管線輸入?

false

接受萬用字元?

false

-UseTransaction

將命令加入使用中交易。只有交易為處理中狀態時,此參數才有效。如需詳細資訊,請參閱 about_Transactions

必要?

false

位置?

具名

預設值

接受管線輸入?

false

接受萬用字元?

false

<CommonParameters>

此 Cmdlet 支援一般參數:-Debug、-ErrorAction、-ErrorVariable、-OutBuffer、-OutVariable、-Verbose、-WarningAction 以及 -WarningVariable。如需詳細資訊,請參閱 about_CommonParameters

輸入和輸出

輸入類型是您可以使用管線處理方式傳遞給 Cmdlet 的物件類型。傳回類型是 Cmdlet 傳回之物件的類型。

輸入

System.String[]

您可以使用管線將檔案系統路徑 (使用引號括起來) 傳送至 Get-ChildItem。

輸出

System.IO.DirectoryInfo、System.IO.FileInfo、System.String

附註

Attributes、Directory、File、Hidden、ReadOnly 及 System 參數是在 Windows PowerShell 3.0 引進,而且

只適用於檔案系統磁碟機。

Get-ChildItem 別名參照:

---------------------------------

Get-ChildItem dir

Directory d、ad

File af

Hidden h、ah

ReadOnly ar

System as

範例 1

C:\PS>Get-ChildItem

Description
-----------
This command gets the files and subdirectories in the current directory. If the current directory does not have child items, the command does not return any results.





範例 2

C:\PS>Get-Childitem -System -File -Recurse

Description
-----------
This command gets system files in the current directory and its subdirectories.





範例 3

C:\PS>Get-ChildItem -Attributes !Directory,!Directory+Hidden

C:\PS> dir -att !d,!d+h

Description
-----------
These command get all files, including hidden files, in the current directory, but exclude subdirectories. The second command uses aliases and abbreviations, but has the same effect as the first.





範例 4

C:\PS>dir -ad

Description
-----------
This command gets the subdirectories in the current directory. It uses the "dir" alias of the Get-ChildItem cmdlet and the "ad" alias of the Directory parameter.





範例 5

C:\PS>Get-ChildItem -File -Attributes !ReadOnly -path C:\ps-test

Description
-----------
This command gets read-write files in the C:\ps-test directory.





範例 6

C:\PS>get-childitem . -include *.txt -recurse -force

Description
-----------
This command gets all of the .txt files in the current directory and its subdirectories. 

The dot (.) represents the current directory. The Include parameter specifies the file name extension. The Recurse parameter directs Windows PowerShell to search for objects recursively, and it indicates that the subject of the command is the specified directory and its contents. The Force parameter adds hidden files to the display.





範例 7

C:\PS>get-childitem c:\windows\logs\* -include *.txt -exclude A*

Description
-----------
This command gets the .txt files in the Logs subdirectory, except for those whose names start with the letter A. It uses the wildcard character (*) to indicate the contents of the Logs subdirectory, not the directory container. Because the command does not include the Recurse parameter, Get-ChildItem does not include the contents of the current directory automatically; you need to specify it.





範例 8

C:\PS>get-childitem -name

Description
-----------
This command retrieves only the names of items in the current directory.





See Also

Concepts

FileSystem 提供者

Other Resources

Clear-Content
Get-Content
Get-ChildItem
Get-Content
Get-Item
Remove-Item
Set-Content
Test-Path