Share via


使用檔案、資料夾和登錄機碼

此範例僅適用於 Windows 平臺。

PowerShell 會使用名詞 項目 來參考 PowerShell 磁碟驅動器上找到的專案。 處理 PowerShell FileSystem 提供者時, 專案 可能是檔案、資料夾或 PowerShell 磁碟驅動器。 列出和使用這些專案是大部分系統管理設定中的重要基本工作,因此我們想要詳細討論這些工作。

列舉檔案、資料夾和登錄機碼

由於從特定位置取得專案集合是這類常見的工作,因此 Cmdlet 是特別設計來傳回容器內找到的所有專案, Get-ChildItem 例如資料夾。

如果您要傳回直接包含在資料夾中 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 命令時會看到的內容,或 ls UNIX 命令殼層中的命令。

您可以使用 Cmdlet 的參數 Get-ChildItem 來執行複雜的清單。 您可以輸入下列命令來查看 Cmdlet 的語法 Get-ChildItem

Get-Command -Name Get-ChildItem -Syntax

這些參數可以混合和比對,以取得高度自定義的輸出。

列出所有自主專案

若要查看 Windows 資料夾內的專案,以及子資料夾中包含的任何專案,請使用的 Get-ChildItemRecurse 參數。 清單會顯示 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-ChilditemName 參數:

PS> Get-ChildItem -Path C:\WINDOWS -Name
addins
AppPatch
assembly
...

強制列出隱藏專案

隱藏在 檔案總管 或cmd.exe未顯示在命令輸出Get-ChildItem中的專案。 若要顯示隱藏的專案,請使用的 Get-ChildItemForce 參數。 例如:

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

排除專案

您可以使用的 Exclude 參數 Get-ChildItem排除特定專案。 這可讓您在單一語句中執行複雜的篩選。

例如,假設您嘗試在 System32 資料夾中尋找 Windows Time 服務 DLL,而您只需要記住 DLL 名稱的開頭為 “W”,並在其中具有 “32”。

之類的 w*32*.dll 表達式會尋找符合條件的所有 DLL,但您可能想要進一步篩選出檔案,並省略任何 win32 檔案。 您可以使用 Exclude 參數搭配模式win*來省略這些檔案

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 參數

您可以在相同的命令中使用 Cmdlet 的 Get-ChildItem 數個參數。 混合參數之前,請確定您瞭解通配符比對。 例如,下列命令不會傳回任何結果:

Get-ChildItem -Path C:\Windows\*.dll -Recurse -Exclude [a-y]*.dll

即使 Windows 資料夾中有兩個以字母 「z」 開頭的 DLL,也沒有結果。

未傳回任何結果,因為我們將通配符指定為路徑的一部分。 即使命令是遞迴的,Cmdlet 仍 Get-ChildItem 會將專案限制為 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