Convert-Path

将路径从 PowerShell 路径转换为 PowerShell 提供程序路径。

语法

Path (默认值)

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

LiteralPath

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

说明

Convert-Path cmdlet 将路径从 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 不返回隐藏项。 此示例使用 Force 参数查找隐藏的项。 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

可以通过管道将路径(但不是文本路径)传递给此 cmdlet。

输出

String

此 cmdlet 返回包含转换路径的字符串。

备注

PowerShell 包含以下与 Convert-Path相关的别名:

  • 所有平台:
    • cvpa

包含 Path 名词的命令行脚本用于操控路径名称,并以所有 PowerShell 提供程序都能解析的简洁格式返回名称。 它们旨在用于要以特定格式显示路径的所有或部分的程序和脚本。 使用它们就像使用 DirnameNormpathRealpathJoin或其他路径操控器一样。

可以将路径 cmdlet 与某些提供程序一起使用,包括 FileSystemRegistryCertificate 提供程序。

此 cmdlet 的设计目的是与任何供应商公开的数据进行协作。 若要列出会话中可用的提供程序,请键入 Get-PSProvider。 有关详细信息,请参阅 about_Providers

Convert-Path 仅转换现有路径。 它不能用于转换尚不存在的位置。