How to normalize a path in PowerShell?

dan-gph picture dan-gph · Jan 30, 2009 · Viewed 83.5k times · Source

I have two paths:

fred\frog

and

..\frag

I can join them together in PowerShell like this:

join-path 'fred\frog' '..\frag'

That gives me this:

fred\frog\..\frag

But I don't want that. I want a normalized path without the double dots, like this:

fred\frag

How can I get that?

Answer

Shay Levy picture Shay Levy · Jan 31, 2009

You can expand ..\frag to its full path with resolve-path:

PS > resolve-path ..\frag 

Try to normalize the path using the combine() method:

[io.path]::Combine("fred\frog",(resolve-path ..\frag).path)