title: PowerShell Executing a function within a Script Block using Start-Process does weird things with double quotes
body: <p>I have a PowerShell script that edits the registry, so it needs to run as admin. To do this, I start up a new PowerShell process from my running PowerShell script, and pass in part of the registry key path using a Script Block with a function in it. When I use double quotes within that function, PowerShell tries to interpret them as commands, rather than a string.  If I use single quotes though, then everything works fine.</p>
  
  <p>I've created a little stripped down sample powershell script that reproduces the problem. Here's the snippet:</p>
  
  <pre><code>$ScriptBlock = {
      function Test
      {
          $status = "This is a string"
          Write-Output $status
      }
  }
  Start-Process -FilePath PowerShell -ArgumentList "-NoExit -NoProfile -ExecutionPolicy Bypass -Command &amp; {$ScriptBlock Test}"
  </code></pre>
  
  <p>So in the new PowerShell process it will first define the code in the script block and then call the Test method, and it produces this error:</p>
  
  <blockquote>
    <p>This : The term 'This' is not recognized as the name of a cmdlet,
    function, script file, or operable program. Check the spelling of the
    name, or if a path was included, verify that the path is correct and
    try again.</p>
  </blockquote>
  
  <p>So it's trying to treat the string as a commad, as if I had just typed <code>This is a string</code> by itself on a new line in my script.</p>
  
  <p>If I change the line</p>
  
  <pre><code>$status = "This is a string"
  </code></pre>
  
  <p>to</p>
  
  <pre><code>$status = 'This is a string'
  </code></pre>
  
  <p>the script works as expected and simply outputs the string <code>This is a string</code>.</p>
  
  <p>Another strange problem I've noticed is that if I don't use a variable and just use:</p>
  
  <pre><code>Write-Output "This is a string"
  </code></pre>
  
  <p>then it outputs each word on a separate line like this:</p>
  
  <blockquote>
    <p>This</p>
    
    <p>is</p>
    
    <p>a</p>
    
    <p>string</p>
  </blockquote>
  
  <p>but if I use single quotes like this:</p>
  
  <pre><code>Write-Output 'This is a string'
  </code></pre>
  
  <p>then it outputs the entire sentence on one line as expected.</p>
  
  <p>Does anybody know why PowerShell is behaving strangely in these situations?</p>
  
  <h2>Answer</h2>
  
  <p>So as TessellatingHeckler mentions, the solution is do wrap anything that is double quoted in double double quotes, single quotes, or you can use brackets.</p>
  
  <p>So in my example, you would change:</p>
  
  <pre><code>$status = "This is a string"
  </code></pre>
  
  <p>to this:</p>
  
  <pre><code>$status = """This is a string"""
  </code></pre>
  
  <p>or this:</p>
  
  <pre><code>$status = '"This is a string"'
  </code></pre>
  
  <p>or this:</p>
  
  <pre><code>$status = {"This is a string"}
  </code></pre>
  
  <p>If you want to evaluate a variable in your string though (i.e. see the variable's value), then you have to go with the double double quotes method:</p>
  
  <pre><code>$status = """This is a string that evaluates $someVariable"""
  </code></pre>
  
  <p>Still not sure if this is a Bug or By Design, but at least we have a workaround, as this fixes both of the problems I described above.</p>