How to normalize a path in PowerShell?

后端 未结 12 2085
半阙折子戏
半阙折子戏 2020-12-05 03:53

I have two paths:

fred\\frog

and

..\\frag

I can join them together in PowerShell like this:



        
12条回答
  •  -上瘾入骨i
    2020-12-05 04:14

    Any non-PowerShell path manipulation functions (such as those in System.IO.Path) will not be reliable from PowerShell because PowerShell's provider model allows PowerShell's current path to differ from what Windows thinks the process' working directory is.

    Also, as you may have already discovered, PowerShell's Resolve-Path and Convert-Path cmdlets are useful for converting relative paths (those containing '..'s) to drive-qualified absolute paths but they fail if the path referenced does not exist.

    The following very simple cmdlet should work for non-existant paths. It will convert 'fred\frog\..\frag' to 'd:\fred\frag' even if a 'fred' or 'frag' file or folder cannot be found (and the current PowerShell drive is 'd:').

    function Get-AbsolutePath {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
            [string[]]
            $Path
        )
    
        process {
            $Path | ForEach-Object {
                $PSCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($_)
            }
        }
    }
    

提交回复
热议问题