Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do a screen capture in Windows PowerShell?

Tags:

powershell

How can I capture the screen in Windows PowerShell? I need to be able to save the screen to disk.

like image 648
Start-Automating Avatar asked Jun 03 '10 19:06

Start-Automating


People also ask

What is the Windows command for screen capture?

Press Windows logo key + Shift + S. The desktop will darken while you select an area for your screenshot.

How do I take a screenshot using command prompt?

Click any window except the command window and then hit PrtScrn. Show activity on this post. First off all open cmd in full screen mode then click on print screen button after that open paint brush and press ctrl + v (past) you can save it in any where, where ever you want (file type should be . png).

How do I screen capture my screen?

Press the Power and Volume down buttons at the same time. If that doesn't work, press and hold the Power button for a few seconds. Then tap Screenshot.


2 Answers

You can also use .NET to take the screenshot programatically (which gives you finer control):

[Reflection.Assembly]::LoadWithPartialName("System.Drawing") function screenshot([Drawing.Rectangle]$bounds, $path) {    $bmp = New-Object Drawing.Bitmap $bounds.width, $bounds.height    $graphics = [Drawing.Graphics]::FromImage($bmp)     $graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size)     $bmp.Save($path)     $graphics.Dispose()    $bmp.Dispose() }  $bounds = [Drawing.Rectangle]::FromLTRB(0, 0, 1000, 900) screenshot $bounds "C:\screenshot.png" 
like image 99
Jeremy Avatar answered Sep 18 '22 13:09

Jeremy


For the sake of completion, this script allows you to take screenshots across multiple monitors.

Base code comes from Jeremy

[Reflection.Assembly]::LoadWithPartialName("System.Drawing") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")  [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  function screenshot($path)  {     $width = 0;     $height = 0;     $workingAreaX = 0;     $workingAreaY = 0;      $screen = [System.Windows.Forms.Screen]::AllScreens;      foreach ($item in $screen)     {         if($workingAreaX -gt $item.WorkingArea.X)         {             $workingAreaX = $item.WorkingArea.X;         }          if($workingAreaY -gt $item.WorkingArea.Y)         {             $workingAreaY = $item.WorkingArea.Y;         }          $width = $width + $item.Bounds.Width;          if($item.Bounds.Height -gt $height)         {             $height = $item.Bounds.Height;         }     }      $bounds = [Drawing.Rectangle]::FromLTRB($workingAreaX, $workingAreaY, $width, $height);      $bmp = New-Object Drawing.Bitmap $width, $height;     $graphics = [Drawing.Graphics]::FromImage($bmp);      $graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size);      $bmp.Save($path);      $graphics.Dispose();     $bmp.Dispose(); } 

Can be called with: screenshot "D:\screenshot.png"

like image 30
Skami Avatar answered Sep 22 '22 13:09

Skami