PowerShell 中 Test-Path 和 Resolve-Path 之間的區別?


Test-Path 命令檢查特定路徑是否存在並返回布林值輸出(True 或 False),而 Resolve-Path 命令顯示特定目錄(如果存在),否則引發異常。例如,

對於存在的路徑,

示例

PS C:\> Test-Path C:\Temp\
True
PS C:\> Resolve-Path C:\Temp\
Path
----
C:\Temp\

對於不存在的路徑,

PS C:\> Test-Path C:\Temp11\
False
PS C:\> Resolve-Path C:\Temp11\
Resolve-Path : Cannot find path 'C:\Temp11\' because it does not exist.
At line:1 char:1
+ Resolve-Path C:\Temp11\
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Temp11\:String) [Resolve-Path], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.ResolvePathCommand

Resolve-Path 還用於獲取使用萬用字元的檔案內容。例如,

示例

Resolve-Path C:\Temp\*

以上命令將獲取 C:\temp 路徑內的所有檔案和資料夾。

Resolve-Path C:\Temp\web*

以上命令將使用以 Web 開頭的關鍵字獲取 C:\temp 內的所有檔案。

輸出

Path
----
C:\Temp\WebImages
C:\Temp\web.html
C:\Temp\web1.html

更新於: 2021 年 2 月 8 日

620 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告