Convert-Path

將 PowerShell 路徑轉換成 PowerShell 提供者路徑。

語法

Path (預設值)

Convert-Path
    [-Path] <String[]>
    [<CommonParameters>]

LiteralPath

Convert-Path
    -LiteralPath <String[]>
    [<CommonParameters>]

Description

Convert-Path指令長將 PowerShell 路徑轉換成 PowerShell 提供者路徑。

範例

範例 1:將工作目錄轉換成標準檔案系統路徑

此範例將目前的工作目錄(以點 (.)表示)轉換為標準的 FileSystem 路徑。

PS C:\> Convert-Path .
C:\

範例 2:將提供者路徑轉換為標準登錄路徑

此範例將 PowerShell 提供者路徑轉換為標準登錄檔路徑。

PS C:\> Convert-Path HKLM:\Software\Microsoft
HKEY_LOCAL_MACHINE\Software\Microsoft

範例 3:將路徑轉換為字串

此範例將目前提供者(FileSystem 提供者)的主目錄路徑轉換為字串。

PS C:\> Convert-Path ~
C:\Users\User01

範例 4:轉換隱藏物品的路徑

根據預設,Convert-Path 不會傳回隱藏的專案。 這個例子使用 力 場參數來尋找隱藏物品。 Get-Item 命令會確認 .git 資料夾已隱藏。 使用不含 Convert-Path 參數的 只會傳回可見的專案。 新增 Force 參數會傳回所有專案,包括隱藏的專案。

PS> Get-Item .git -Force

    Directory: D:\Git\PS-Docs\PowerShell-Docs

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d--h-           9/25/2024  4:46 PM                .git

PS> Convert-Path .git*
D:\Git\PS-Docs\PowerShell-Docs\.github
D:\Git\PS-Docs\PowerShell-Docs\.gitattributes
D:\Git\PS-Docs\PowerShell-Docs\.gitignore

PS> Convert-Path .git* -Force
D:\Git\PS-Docs\PowerShell-Docs\.git
D:\Git\PS-Docs\PowerShell-Docs\.github
D:\Git\PS-Docs\PowerShell-Docs\.gitattributes
D:\Git\PS-Docs\PowerShell-Docs\.gitignore

參數

-Force

允許 Cmdlet 取得使用者無法存取的項目,例如隱藏或系統檔案。 Force 參數不會覆寫安全性限制。 實作會因提供者而異。 如需詳細資訊,請參閱 about_Providers。

此參數已在 PowerShell 7.5-preview.5 中新增。

參數屬性

類型:SwitchParameter
預設值:False
支援萬用字元:False
不要顯示:False

參數集

(All)
Position:Named
必要:False
來自管線的值:False
來自管線按屬性名稱的值:False
來自剩餘引數的值:False

-LiteralPath

以字串陣列的形式指定要轉換的路徑。 LiteralPath 參數的值會與類型完全相同。 不會將任何字元解譯為通配符。 如果路徑包含逸出字元,請以單引弧括住它。 單引號會告知PowerShell不要將任何字元解譯為逸出序列。

如需詳細資訊,請參閱 about_Quoting_Rules。

參數屬性

類型:

String[]

預設值:None
支援萬用字元:False
不要顯示:False
別名:PSPath, LP

參數集

LiteralPath
Position:Named
必要:True
來自管線的值:False
來自管線按屬性名稱的值:True
來自剩餘引數的值:False

-Path

指定要轉換的 PowerShell 路徑。

參數屬性

類型:

String[]

預設值:None
支援萬用字元:True
不要顯示:False

參數集

Path
Position:0
必要:True
來自管線的值:True
來自管線按屬性名稱的值:True
來自剩餘引數的值:False

CommonParameters

此 cmdlet 支援常見參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。

輸入

String

你可以用管道路徑(pipe)傳送到這個指令檔,但不能直接傳送路徑。

輸出

String

此 cmdlet 會回傳包含轉換路徑的字串。

備註

PowerShell 包含下列 Convert-Path的別名:

  • 所有平臺:
    • cvpa

包含 Path 名詞的 Cmdlet 會作路徑名稱,並以簡潔的格式傳回名稱,讓所有 PowerShell 提供者都能解譯。 它們是設計給程式和腳本,當你想以特定格式顯示全部或部分路徑時。 像使用 Dirname、 Normpath、 Realpath、 Join或其他路徑操控器一樣使用它們。

你可以將路徑指令匣搭配多種提供者使用,包括 檔案系統、 登錄檔和 憑證 提供者。

此 Cmdlet 的設計目的是要處理任何提供者所公開的數據。 若要列出工作階段中可用的提供者,請輸入 Get-PSProvider。 如需詳細資訊,請參閱 about_Providers。

Convert-Path 只會轉換現有路徑。 它無法用來轉換尚未存在的地點。