Reputation: 942
I need to join two paths, each of those can be absolute. In such a case the second path should win, if both are. But the Join-Path
commandlet doesn't seem to produce the desired result:
> Join-Path 'path' 'C:\Windows'
path\C:\Windows
The result produced is invalid as long as the 2d path is absolute no matter what I tried, quite surprisingly for me, because I guess I'm too used to path joining facilities in other languages where this would result in C:\Windows
.
How can I solve this? I use Powershell Core v7.1.
Upvotes: 2
Views: 231
Reputation: 61148
If you want that behavior in PowerShell, don't use the Join-Path
cmdlet, because it is merely combining the Path
parameter together with the ChildPath
parameter (in that order) using the backslash and makes sure these backslashes aren't doubled in doing so.
The alternative that does do what you expect is to use .NET
[System.IO.Path]::Combine('path','C:\Windows') # --> C:\Windows
[System.IO.Path]::Combine('D:\somewhere\path','C:\Windows') # --> C:\Windows
Upvotes: 2