title: Converting batch file to powershell with special characters in path
body: <p>I'm having a hard time to write a simple batch file as powershell script.</p>
  
  <p>Consider this folder structure. Note the directory with the cool <code>[1]</code> in it...
  <img src="https://i.stack.imgur.com/j7yyb.png" alt="enter image description here"></p>
  
  <p><a href="https://exiftool.org/" rel="nofollow noreferrer"><strong>exiftool.exe</strong></a><br>
  Is a command utility to (for example) extract pictures from embedded MP3 tags.<br>
  I <a href="https://gist.github.com/anonymous/f723b55c5038f28de8f3" rel="nofollow noreferrer">uploaded its help</a> if you need more info.</p>
  
  <p><strong>oldscript.cmd</strong><br>
  <code>exiftool -picture -b input.mp3 &gt; output.jpg</code><br>
  This line is the one to write in powershell. I found the syntax in a <a href="https://exiftool.org/forum/index.php/topic,3856.msg17838.html" rel="nofollow noreferrer">forum post</a> from the author  </p>
  
  <ul>
  <li><code>-picture</code> stands for the tag to extract and <code>-b</code> stands for binary mode</li>
  <li><code>input.mp3</code> is my test mp3 which can contain special characters in its path like [ and ]</li>
  <li><code>&gt; output.jpg</code> defines the name and saves the resulting image in the same folder</li>
  </ul>
  
  <p><strong>newscript.ps1</strong><br>
  My best current non-working code is:  </p>
  
  <pre><code>$ownpath = Split-Path $MyInvocation.MyCommand.Path
  $exe = $ownpath + '\exiftool.exe'
  $input = $ownpath + '\input.mp3'
  $outimg = $ownpath + '\output.jpg'    
  
  &amp; $exe -picture -binary $input| Set-Content -literalPath $outimg -encoding UTF8
  </code></pre>
  
  <p>I found <code>Set-Content</code> which is able to handle special characters in pathes through "-literalpath". But I'm still not able to convert the batch to a Powershell script because
  Set-Content (and Out-File method too) seems work different compared to old batch piping (">"). The resulting image is not viewable regardless which encoding I use. The <a href="https://gist.github.com/anonymous/f723b55c5038f28de8f3" rel="nofollow noreferrer">help file from above</a> says that exiftool is using UTF8 encoding.</p>
  
  <p>Of course I tried other <a href="http://ss64.com/ps/set-content.html" rel="nofollow noreferrer">available encodings</a>, but all of them failed to produce a viewable image. I'm stuck at this point. So my initial question still stands partly "How do I convert this batch file to powershell".</p>
  
  <p><strong>So why is it working when using the old batch command?</strong></p>
  
  <p>For example: create a folder "D:folder" and place this <a href="http://ge.tt/4PUs8hY/v/1" rel="nofollow noreferrer">MP3 file with a cover image</a> in it.<br>
  Download <a href="https://exiftool.org/" rel="nofollow noreferrer">exiftool.exe</a> from above and place it there too.</p>
  
  <p>The old batch command will work and give you a viewable image  </p>
  
  <pre><code>D:\folder\exiftool -picture -binary D:\folder\input.mp3 &gt; D:\folder\output.jpg
  </code></pre>
  
  <p>The new Powershell V2 script with the same syntax will fail. Why?   </p>
  
  <pre><code>&amp; D:\folder\exiftool.exe -picture -binary D:\folder\input.mp3 &gt; D:\folder\output.jpg
  </code></pre>