Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Normalized file path comparison in Powershell

Say I have the following two paths (both are valid, say):

$p1 = "D:\folder1\"
$p2 = "D:\Folder1"

I want to compare these two paths for equality. I am expecting both paths to be the same. I tried both the commands below:

  • (Resolve-Path $p1) -eq (Resolve-Path $p2)
  • (Get-Item $p1) -eq (Get-Item $p2)

None returned $True because the backslash was not normalized. Now I know I can do some regex magic and make it work but all I am looking for is an in-built solution for comparing file paths.

like image 320
bittusarkar Avatar asked Nov 01 '25 15:11

bittusarkar


1 Answers

join-path "C:\Windows" ""
join-path "C:\Windows\" ""

Both return "C:\Windows\" since join-path normalizes them.

like image 73
Trondh Avatar answered Nov 04 '25 10:11

Trondh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!