title: How can I base64 encode a string on linux so it matches windows "Unicode.GetBytes.ToBase64String"?
body: <p>This question probably has been answered, but I couldn't find it</p>
  
  <p><strong>Question</strong></p>
  
  <p>How can I 'base64 encode' a string in bash so it matches "what windows expects", i.e.  "Unicode.GetBytes.ToBase64String"</p>
  
  <p><strong>Context</strong></p>
  
  <p>The powershell help text has this example</p>
  
  <pre><code>$command='dir'
  $bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
  $encodedCommand = [Convert]::ToBase64String($bytes)
  echo $encodedCommand
  powershell.exe -encodedCommand $encodedCommand
  </code></pre>
  
  <p><strong>Windows Output</strong></p>
  
  <p>The encoded output is this:</p>
  
  <pre><code>ZABpAHIA
  </code></pre>
  
  <p><strong>Very Different results on linux</strong></p>
  
  <p>If I do the same thing on linux, I get this:</p>
  
  <pre><code>echo "dir" | base64
  ZGlyCg==
  </code></pre>
  
  <p><strong>Encodings?</strong></p>
  
  <p>I've tried using "iconv" to convert encodings, but no luck, i.e same results.</p>
  
  <p>echo dir |iconv -f ASCII -t UTF-8   |base64</p>
  
  <p><strong>Differences</strong></p>
  
  <p>So you can see two differences:</p>
  
  <ul>
  <li>Alphanumeric characters differ</li>
  <li>Trailing "===" appears on linux</li>
  </ul>