title: Encoding big command block with variables and multiple lines for use with cmd
body: <p>I'm trying to use encoding for fileless deployment of scripts but I've hit a wall of encoding.</p>
  <p>After using Base64, I'm getting comfortable string without any special characters which would require escaping. However all variables from a command block disappear.</p>
  <p>Currently I'm using such method:</p>
  <ul>
  <li>I'm encoding script, then I can easily divide (9k character limit in
  cmd) it with Notepad++ and feed into management program, I'm using standard Base64 from powershell:</li>
  </ul>
  <p><code>$encoded = [Convert]::ToBase64String([System.Text.Encoding]::UTF.GetBytes($toEncode))</code></p>
  <ul>
  <li>Managementprogram starts a windows command line which writes encoded values to temp file and
  starts &quot;powershell -enc %file&quot;</li>
  </ul>
  <p>It works well for simple commands, but I want to use it with much more complex scripts with additional C# snippets - multiple lines, here-strings, pretty much all special characters used in both languages. And in this case, variables simply dissapear (ie.: instead of <code>foreach($test in $tests)</code> there is <code>foreach ( in )</code> in the result)</p>
  <p><strong>Summarizing:</strong> I'm looking for a method to encode complex script into a string without special characters. Method have to be built in or available as one-liner in Powershell. Requirements:</p>
  <ul>
  <li>I can freely use &quot;powershell -command&quot;, but only as one-liners (no way to use here-strings because of cmd involved)</li>
  <li>I cannot use any additional modules (offline system).</li>
  <li>I would prefer to avoid any special characters that require escaping in cmd. In worst case small number of such characters is acceptable</li>
  </ul>