Created
August 15, 2019 14:33
-
-
Save randomchance/8e4e02f35e5cbd7e2cfb514c4179d8b0 to your computer and use it in GitHub Desktop.
powershell function to resolve a file path, expanding any mapped dives to their full UNC path in the process.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Resolve-FullPath | |
{ | |
[CmdletBinding()] | |
[OutputType([string])] | |
param ( | |
# The Path to the File or Folder | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] | |
[Alias("FullName")] | |
[ValidateNotNullOrEmpty()] | |
[string] | |
$Path | |
) | |
<# | |
.SYNOPSIS | |
Resolves the full path to a file or directory, including the UNC path of any mapped drives. | |
.PARAMETER Path | |
The Path you want to resolve. | |
.OUTPUTS | |
The fully resolved path as a string. | |
.EXAMPLE | |
PS> Resolve-FullPath -Path "Z:\tools\Default-BGInfo.bgi" | |
\\storage.lab.local\3rdPartytools\Default-BGInfo.bgi | |
#> | |
process | |
{ | |
$Location = Get-Item -Path $Path | |
if (-not [string]::IsNullOrEmpty( $Location.PSDrive.DisplayRoot)) | |
{ | |
$CorrectRoot = "{0}\" -f $Location.PSDrive.DisplayRoot | |
Write-Verbose ("The drive root [{0}] resolves to {1}" -f $Location.PSDrive.Root, $CorrectRoot) | |
$Location.FullName.Replace($Location.PSDrive.Root, $CorrectRoot) | |
} | |
else | |
{ | |
$Location.FullName | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment