title: Powershell, Save web image using outvariable
body: <p>I want to capture a web image into a variable for processing (see below).  I want to avoid saving it to a file, processing the file, then deleting the file.  I can save the web-request output directly to a file, as shown, but if I save the output to a variable then save the variable content to a file, I do not have the same result.  </p>
  
  <pre><code>    # saving output to a file works
          $image='https://www.google.com/images/srpr/logo11w.png'
          Invoke-WebRequest $image -outfile image1.png
  
      &lt;# I want to skip creating a file, using it, then deleting it
      Save the image into a variable, convert to base64 to store in db, retrieve from db, convert back, save to a file
      #&gt;
          Invoke-WebRequest $image -OutVariable imagevariable
          $imagevariable | out-file image2.png   # this is not a valid image
  
      # Convert to base64 (using PSCX add-on) to store string
          $toBase64=ConvertTo-Base64($imagevariable)
  
      # save to, retrieve from db
          #Invoke-Sqlcmd insert...
          #Invoke-Sqlcmd select...
  
      # Convert from base64 (using PSCX add-on)  &amp; output to a file
          $fromBase64 | ConvertFrom-Base64 -OutputPath image3.png
  </code></pre>
  
  <p>For ease, I am going to use the <a href="https://pscx.codeplex.com/" rel="nofollow">PSCX community extensions</a> for base64 conversion.</p>