此範例僅適用於 Windows 平臺。
PowerShell 會使用名詞 Item 來參照 PowerShell 磁碟機上找到的項目。 處理 PowerShell FileSystem 提供者時,Item 可能是檔案、資料夾或 PowerShell 磁碟。 列出和使用這些專案是大部分系統管理設定中的重要基本工作,因此我們想要詳細討論這些工作。
列舉檔案、資料夾和登錄機碼
由於從特定位置取得項目集合是一項非常常見的任務,因此 Get-ChildItem Cmdlet 是專門設計用於回傳容器內(例如資料夾)發現的所有項目。
如果您要傳回直接位於資料夾 C:\Windows內的所有檔案和資料夾,請輸入:
PS> Get-ChildItem -Path C:\Windows
Directory: Microsoft.PowerShell.Core\FileSystem::C:\Windows
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2006-05-16 8:10 AM 0 0.log
-a--- 2005-11-29 3:16 PM 97 acc1.txt
-a--- 2005-10-23 11:21 PM 3848 actsetup.log
...
當您在 cmd.exe中輸入 dir 命令或 Unix 命令殼層中的 ls 命令時,清單看起來會類似您所看到的內容。
您可以使用 Get-ChildItem Cmdlet 的參數來執行複雜的清單。 您可以輸入下列命令來檢視 Get-ChildItem Cmdlet 的語法:
Get-Command -Name Get-ChildItem -Syntax
這些參數可以混合和比對,以取得高度自定義的輸出。
列出所有包含的項目
若要查看 Windows 資料夾內的專案,以及子資料夾中包含的任何專案,請使用 Get-ChildItem的 Recurse 參數。 清單會顯示 Windows 資料夾中的所有專案,以及其子資料夾中的專案。 例如:
PS> Get-ChildItem -Path C:\WINDOWS -Recurse
Directory: Microsoft.PowerShell.Core\FileSystem::C:\WINDOWS
Directory: Microsoft.PowerShell.Core\FileSystem::C:\WINDOWS\AppPatch
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2004-08-04 8:00 AM 1852416 AcGenral.dll
...
依名稱篩選項目
若要只顯示項目的名稱,請使用 Get-ChildItem的 Name 參數:
PS> Get-ChildItem -Path C:\WINDOWS -Name
addins
AppPatch
assembly
...
強制列出隱藏項目
檔案總管或 cmd.exe 中隱藏的專案不會顯示在 Get-ChildItem 命令的輸出中。 若要顯示隱藏項目,請使用 Get-ChildItem的 Force 參數。
例如:
Get-ChildItem -Path C:\Windows -Force
這個參數名為 Force,是因為您可以強制覆蓋 Get-ChildItem 命令的正常行為。
Force 是一個廣泛使用的參數,可強制 Cmdlet 通常不會執行的動作,但無法執行任何危害系統安全性的動作。
使用通配符比對項目名稱
Get-ChildItem 命令會接受要列出之項目路徑中的通配符。
因為通配符比對是由PowerShell引擎處理,因此接受通配符的所有 Cmdlet 都會使用相同的表示法,而且具有相同的比對行為。 PowerShell 通配符表示法包括:
- 星號(
*)可以匹配任何字元的零次或多次出現。 - 問號 (
?) 完全符合一個字元。 - 左括弧 (
[) 字元和右括弧 (]) 字元圍繞一組要比對的字元。
以下是通配符規格運作方式的一些範例。
若要在 Windows 目錄中尋找後綴為 .log 且基底名稱中只有五個字元的所有檔案,請輸入下列命令:
PS> Get-ChildItem -Path C:\Windows\?????.log
Directory: Microsoft.PowerShell.Core\FileSystem::C:\Windows
Mode LastWriteTime Length Name
---- ------------- ------ ----
...
-a--- 2006-05-11 6:31 PM 204276 ocgen.log
-a--- 2006-05-11 6:31 PM 22365 ocmsn.log
...
-a--- 2005-11-11 4:55 AM 64 setup.log
-a--- 2005-12-15 2:24 PM 17719 VxSDM.log
...
若要尋找以 Windows 目錄中字母 x 開頭的所有檔案,請輸入:
Get-ChildItem -Path C:\Windows\x*
若要尋找名稱開頭為 「x」 或 「z」 的所有檔案,請輸入:
Get-ChildItem -Path C:\Windows\[xz]*
如需通配符的詳細資訊,請參閱 about_Wildcards。
排除項目
您可以使用 Get-ChildItem的 Exclude(排除) 參數排除特定項目。 這可讓您在單一語句中執行複雜的篩選。
例如,假設您嘗試在 System32 資料夾中尋找 Windows Time 服務 DLL,而您唯一記得的有關 DLL 名稱的資訊是:名稱的開頭為 “W”,並包含 “32”。
像是 w*32*.dll 的表示式會尋找符合條件的所有 DLL,但您可能想要進一步篩選出檔案,並省略任何 win32 檔案。 您可以使用具有模式 win*的 Exclude 參數來省略這些檔案:
PS> Get-ChildItem -Path C:\WINDOWS\System32\w*32*.dll -Exclude win*
Directory: C:\WINDOWS\System32
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 3/18/2019 9:43 PM 495616 w32time.dll
-a--- 3/18/2019 9:44 PM 35328 w32topl.dll
-a--- 1/24/2020 5:44 PM 401920 Wldap32.dll
-a--- 10/10/2019 5:40 PM 442704 ws2_32.dll
-a--- 3/18/2019 9:44 PM 66048 wsnmp32.dll
-a--- 3/18/2019 9:44 PM 18944 wsock32.dll
-a--- 3/18/2019 9:44 PM 64792 wtsapi32.dll
混合 Get-ChildItem 參數
您可以在相同的命令中使用 Get-ChildItem Cmdlet 的數個參數。 混合參數之前,請確定您瞭解通配符比對。 例如,下列命令不會傳回任何結果:
Get-ChildItem -Path C:\Windows\*.dll -Recurse -Exclude [a-y]*.dll
即使 Windows 資料夾中有兩個以字母 「z」 開頭的 DLL,也沒有結果。
未傳回任何結果,因為我們將通配符指定為路徑的一部分。 即使命令是遞歸的,Get-ChildItem Cmdlet 仍會將專案限制為 Windows 資料夾中名稱結尾為 .dll的專案。
若要為名稱符合特殊模式的檔案指定遞歸搜尋,請使用 Include 參數。
PS> Get-ChildItem -Path C:\Windows -Include *.dll -Recurse -Exclude [a-y]*.dll
Directory: Microsoft.PowerShell.Core\FileSystem::C:\Windows\System32\Setup
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2004-08-04 8:00 AM 8261 zoneoc.dll
Directory: Microsoft.PowerShell.Core\FileSystem::C:\Windows\System32
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2004-08-04 8:00 AM 337920 zipfldr.dll