{"id":152,"date":"2016-05-05T00:00:00","date_gmt":"2013-01-01T00:00:00","guid":{"rendered":"https:\/\/www.sqlhammer.com\/compare-paths-with-powershell"},"modified":"2026-03-18T21:52:32","modified_gmt":"2026-03-18T21:52:32","slug":"compare-paths-with-powershell","status":"publish","type":"post","link":"https:\/\/sqlhammer.com\/index.php\/2016\/05\/05\/compare-paths-with-powershell\/","title":{"rendered":"Compare paths with PowerShell"},"content":{"rendered":"<p>The other day I came across the need to compare two different paths for equality. The most annoying part of this task is when the paths are directories and you need to worry about the trailing slash. PowerShell provides a simple solution with minimal code.<\/p>\n<h2>First let\u2019s look at the manual\u00a0method<\/h2>\n<pre><code class=\"language-\">#Paths\n$path1 = \"C:\\fake\\\"\n$path2 = \"C:\\fake\"\n\n<p>#Match path formats\nif($path1.Substring($path1.Length - 1, 1).CompareTo(\"\\\") -ne 0)\n{\n$path1 = $path1 + \"\\\";\n}\nif($path2.Substring($path2.Length - 1, 1).CompareTo(\"\\\") -ne 0)\n{\n$path2 = $path2 + \"\\\";\n}<\/p>\n\n<p>#Compare paths\n$path1\n$path2\n$path1.CompareTo($path2) -eq 0<\/code><\/pre>\n<\/p>\n<h3>Results<\/h3>\n<p><a href=\"http:\/\/www.sqlhammer.com\/?attachment_id=3015\"><img decoding=\"async\" src=\"http:\/\/www.sqlhammer.com\/wp-content\/uploads\/2016\/05\/manualresults.png\" alt=\"\" \/><\/a><\/p>\n<p>That is a bunch of code and it took me a couple\u00a0minutes to write. PowerShell\u2019s <a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/hh849799.aspx\">Join-Path<\/a> cmdlet, however, will handle formatting for you.<\/p>\n<h2>Join-Path method<\/h2>\n<pre><code class=\"language-\">#Paths\n$path1 = \"C:\\fake\\\"\n$path2 = \"C:\\fake\"\n\n<p>#Compare paths\n(Join-Path $path1 '') -eq (Join-Path $path2 '')<\/code><\/pre>\n<\/p>\n<h3>Results<\/h3>\n<p><a href=\"http:\/\/www.sqlhammer.com\/?attachment_id=3016\"><img decoding=\"async\" src=\"http:\/\/www.sqlhammer.com\/wp-content\/uploads\/2016\/05\/joinpathresults.png\" alt=\"\" \/><\/a><\/p>\n<h2>Wrap up<\/h2>\n<p>It is obvious that the Join-Path method is easier to remember and faster to type. I use this method every time that I compare paths, even if they are full paths with file names.<\/p>\n<p>The output of the Join-Path cmdlet can seem odd when using full paths but the comparison still functions properly.<\/p>\n<pre><code class=\"language-\">#Paths\n$path1 = \"C:\\fake\\text.txt\"\n$path2 = \"C:\\fake\\text.txt\"\n\n<p>#Compare paths\n(Join-Path $path1 '')\n(Join-Path $path2 '')\n(Join-Path $path1 '') -eq (Join-Path $path2 '')<\/code><\/pre>\n<\/p>\n<p><a href=\"http:\/\/www.sqlhammer.com\/?attachment_id=3017\"><img decoding=\"async\" src=\"http:\/\/www.sqlhammer.com\/wp-content\/uploads\/2016\/05\/fullpathresults.png\" alt=\"\" \/><\/a><\/p>\n<p>The Join-Path cmdlet will add a backslash because it is expecting to have to separate the left and right halves of the path. This makes the path invalid, technically, but it still works for comparison purposes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The other day I came across the need to compare two different paths for equality. The most annoying part of this task is when the paths are directories and you need to worry about the trailing slash. PowerShell provides a simple solution with minimal code. First let\u2019s look at the manual\u00a0method #Paths $path1 = &#8220;C:\\fake\\&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,13,15,16,21],"tags":[],"class_list":["post-152","post","type-post","status-publish","format-standard","hentry","category-administration","category-microsoft-sql-server","category-powershell","category-sql-agent","category-t-sql"],"_links":{"self":[{"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/posts\/152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/comments?post=152"}],"version-history":[{"count":1,"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/posts\/152\/revisions"}],"predecessor-version":[{"id":414,"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/posts\/152\/revisions\/414"}],"wp:attachment":[{"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/media?parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/categories?post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/tags?post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}