Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

Ways to convert relative paths to absolute paths

Some methods (e.g. System.IO.File.WriteAllLines()) needs absolute paths for parameters, so you have to convert from relative paths to absolute paths. In PowerShell, there is more than one way to do it ($RelativePath contains relative path string):

# methods or cmdlets use after Set-Location (change current directory) use for currently non-existent path remove “..\” use for non-existent drive letter
1 [System.IO.Path]::GetFullPath($RelativePath)
2 Convert-Path $RelativePath
3 Resolve-Path $RelativePath
4 [System.IO.Path]::Combine($pwd, $RelativePath)
5 $ExecutionContext.SessionState.Path

If you want to make a path for new files, I recommend to use #4 (get absolute path) and pass the result to #1 (remove “”..\“).