title: Remove double quotes from path variable in PowerShell
body: <p>I'm using PowerShell 5.1.</p>
  <p>I have a script that retrieves the HKLM:\SYSTEM\CurrentControlSet\Services&lt;service&gt;\ImagePath value from the registry.  The string that gets returns looks like this:</p>
  <p><code>&quot;C:\Program Files\Dir1\Dir2&quot;</code></p>
  <p>Note that the double quotes are part of the string.</p>
  <p>I need to get the parent directory for the path represented by this string.  I tried <code>Split-Path</code>, but this leaves the leading double quote in the returned value, which causes <code>Test-Path</code> to return <code>false</code> on a directory that does indeed exist.  For example,</p>
  <pre><code>$key = &quot;HKLM:\SYSTEM\CurrentControlSet\Services\&lt;service&gt;&quot;
  $val = &quot;ImagePath&quot;
  $dir = Get-RegistryValue $key $val
  : &quot;C:\Program Files\Dir1\Dir2&quot;
  $parentDir = Split-Path $dir
  : &quot;C:\Program Files\Dir1  &lt;--leading double quote
  Test-Path $parentDir
  : False
  </code></pre>
  <p>To get around this, I tried using the <code>Trim()</code> call to remove any leading or trailing double-quotes, but that only seems to remove the leading double quote, i.e., the trailing double quote remains in place.  This looks like a bug to me based on my experience with <code>String.Trim()</code> in C#.</p>
  <pre><code>$key = &quot;HKLM:\SYSTEM\CurrentControlSet\Services\&lt;service&gt;&quot;
  $val = &quot;ImagePath&quot;
  $dir = (Get-RegistryValue $key $val).Trim(&quot;`&quot;&quot;)
  : C:\Program Files\Dir1\Dir2&quot;  &lt;-- trailing double quote
  </code></pre>
  <p>The only thing that seems to work is to replace the double quotes with an empty string, like so:</p>
  <pre><code>$key = &quot;HKLM:\SYSTEM\CurrentControlSet\Services\&lt;service&gt;&quot;
  $val = &quot;ImagePath&quot;
  $dir = (Get-RegistryValue $key $val).Replace(&quot;`&quot;&quot;, &quot;&quot;)
  : C:\Program Files\Dir1\Dir2  &lt;-- no double quotes
  </code></pre>
  <p>However, this just feels wrong to me.  It works in this case given that paths cannot have double quotes, i.e., the double quote character is an illegal path variable.  But what if the returned registry value legitimately had a double quote in the string?  This brute approach would remove that double quote.</p>
  <p>I'm still new to PowerShell, so it's very possible that I'm overlooking something that will do what I want to do without requiring me to use the <code>Replace()</code> call.  Any help is greatly appreciated.</p>