title: Save Image from clipboard using PowerShell
body: <p>I am trying to save image from clipboard to the file path. I have tried below script and it is returning "clipboard does not contain image data".</p>
  
  <pre><code>Add-Type -AssemblyName System.Windows.Forms
  if ($([System.Windows.Forms.Clipboard]::ContainsImage())) {
      $image = [System.Windows.Forms.Clipboard]::GetImage()
      $filename='e:\test\test.png'         
  
      [System.Drawing.Bitmap]$image.Save($filename, [System.Drawing.Imaging.ImageFormat]::Png)
      Write-Output "clipboard content saved as $filename"
  } else {
      Write-Output "clipboarsd does not contains image data"
  }
  </code></pre>
  
  <p>As the <code>Clipboard</code> class can only be used in threads set to single thread apartment (STA) mode.</p>
  
  <p>I have tried to run the script in </p>
  
  <pre><code>powershell -NoProfile -Sta -File $file
  </code></pre>
  
  <p>Also, I have tried to relaunch if runspace is not STA, this didn't help.</p>
  
  <pre><code>Add-Type -AssemblyName System.Windows.Forms
  if ($host.Runspace.ApartmentState -ne "STA") {
      "Relaunching"
      $file = "./saveImage.ps1"
      powershell -NoProfile -Sta -File $file 
      return
  }
  </code></pre>