如何在PowerShell中将相对路径转换为绝对路径?
$path1 = C:/1/2/3/
$parth2 = ../../base/
=====
$result = C:/1/base/
$path1 = C:/1/2/3/
$parth2 = ../../base/
=====
$result = C:/1/base/
(Join-Path $path1 $path2) | Resolve-Path
[IO.Path]::GetFullPath(".\abc")
$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(".\abc.txt")
[System.IO.Path]::GetFullPath($BasePath, $RelativePath)
# OR relative to current dir when ($BasePath -eq $PWD):
[System.IO.Path]::GetFullPath($RelativePath)
我觉得我明白你想要的。但是如果这不是你想要的,请随意提供更多信息。
$result = Get-Item $path2 | Select-Object PSPath -ExpandProperty PSPath
$result.Substring(38)
这应该给你一个绝对路径。它会去掉 "Microsoft.Powershell.Core\Filesystem::" 这部分,如果你使用的是 Powershell 5 或更早版本,它可能会有所不同。但是,如果你想要更多细节,可以尝试使用 Get-Item cmdlet 来查看它的显示方式。
(Get-Item somefile.txt | Resolve-Path).ProviderPath
来选择路径,而不带有"Microsoft.Powershell.Core\Filesystem::"
前缀。 - Stephanvs