title: Capture output from a PowerShell command in a batch file's for /f loop
body: <p>This PowerShell code selects the correct value for <code>var5</code> from this string. The desired result it "Alert Raised".</p>
  
  <pre><code>PS C:\src\t&gt; $s = 'Status 58   var5=Alert Raised on: March'
  PS C:\src\t&gt; $s
  Status 58   var5=Alert Raised on: March
  PS C:\src\t&gt; $s | Where-Object { $_ -match '.*var5=(.*)\s+\w+:' } | ForEach-Object { $Matches[1] }
  Alert Raised
  </code></pre>
  
  <p>However, using the same powershell code in a cmd shell script produces a different result. Why is that? Is there something that needs to be escaped for the cmd shell?</p>
  
  <pre><code>C:&gt;type mat002-annex.bat
  @ECHO OFF
  SET "S=Status 58   var5=Alert Raised on: March"
  ECHO S is set to %S
  FOR /F %%a IN ('powershell -NoLogo -NoProfile -Command ^
      " '%S%' | Where-Object { $_ -match '.*var5=(.*)\s+\w+:' } | ForEach-Object { $Matches[1] } "') DO (ECHO Result is "%%a")
  
  C:&gt;mat002-annex.bat
  S is set to S
  Result is "Alert"
  </code></pre>