title: How to get the Java version in PowerShell
body: <p>I'm trying to get the Java version in PowerShell. The version string is printed to <strong>stderr</strong>, so I'm trying to redirect it to stdout and assign it to a string variable.</p>
  
  <p>I get the following strange error:</p>
  
  <pre><code>PS P:\&gt; &amp; java -version 2&gt;&amp;1
  java.exe : java version "1.7.0_25"
  At line:1 char:2
  + &amp; &lt;&lt;&lt;&lt;  java -version 2&gt;&amp;1
      + CategoryInfo          : NotSpecified: (java version "1.7.0_25":String) [], RemoteException
      + FullyQualifiedErrorId : NativeCommandError
  
  Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
  Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
  </code></pre>
  
  <p>Call without redirection (2>&amp;1) gives this:</p>
  
  <pre><code>PS P:\&gt; &amp; java -version
  java version "1.7.0_25"
  Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
  Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
  </code></pre>
  
  <p>I think that Java here is irrelevant, and the same would happen for any other program printing strings to stderr.</p>
  
  <p>The PowerShell version I use is <strong>2.0</strong>.</p>
  
  <p><strong>Questions:</strong></p>
  
  <ul>
  <li>How can I redirect stderr to a variable?</li>
  <li>Or, alternatively, how can I check the installed Java version?</li>
  </ul>
  
  <p><strong>Workaround</strong></p>
  
  <p>I can run it like this:</p>
  
  <pre><code>$output = &amp; cmd /c "java -version 2&gt;&amp;1"
  </code></pre>
  
  <p>But I hate running a cmd.exe where it shouldn't be necessary.</p>