How can I capture the screen in Windows PowerShell? I need to be able to save the screen to disk.
5 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"
-
1Great - how hard would it be to get this to capture the window of a specific application ? I am creating an IE instance like this : $ie = new-object -com "InternetExplorer.Application" - would it be possible to capture just the output of this window for instance ? Mar 14, 2013 at 12:04
-
-
@user310291 the code itself still works, but you need to change the path from "C:\screenshot.png" to some location you can write to, e.g. your desktop.– DreamerJul 23, 2018 at 8:00
For the sake of completion, this script allows you to take screenshots across multiple monitors.
The base code comes from Jeremy.
function screenshot($path)
{
[void] [Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$left = [Int32]::MaxValue
$top = [Int32]::MaxValue
$right = [Int32]::MinValue
$bottom = [Int32]::MinValue
foreach ($screen in [Windows.Forms.Screen]::AllScreens)
{
if ($screen.Bounds.X -lt $left)
{
$left = $screen.Bounds.X;
}
if ($screen.Bounds.Y -lt $top)
{
$top = $screen.Bounds.Y;
}
if ($screen.Bounds.X + $screen.Bounds.Width -gt $right)
{
$right = $screen.Bounds.X + $screen.Bounds.Width;
}
if ($screen.Bounds.Y + $screen.Bounds.Height -gt $bottom)
{
$bottom = $screen.Bounds.Y + $screen.Bounds.Height;
}
}
$bounds = [Drawing.Rectangle]::FromLTRB($left, $top, $right, $bottom);
$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();
}
It can be called with: screenshot "D:\screenshot.png"
-
This solution will be thrown off by DPI scaling. To avoid that, you'd p/invoke. And even then, getting per-monitor scaling information is tricky. See stackoverflow.com/questions/69786234/disable-high-dpi-scaling– halter73Jun 28 at 21:59
This PowerShell function will capture the screen in PowerShell and save it to an automatically numbered file. If the -OfWindow switch is used, then the current window will be captured.
This works by using the built in PRINTSCREEN / CTRL-PRINTSCREEEN tricks, and it uses a bitmap encoder to save the file to disk.
function Get-ScreenCapture
{
param(
[Switch]$OfWindow
)
begin {
Add-Type -AssemblyName System.Drawing
$jpegCodec = [Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() |
Where-Object { $_.FormatDescription -eq "JPEG" }
}
process {
Start-Sleep -Milliseconds 250
if ($OfWindow) {
[Windows.Forms.Sendkeys]::SendWait("%{PrtSc}")
} else {
[Windows.Forms.Sendkeys]::SendWait("{PrtSc}")
}
Start-Sleep -Milliseconds 250
$bitmap = [Windows.Forms.Clipboard]::GetImage()
$ep = New-Object Drawing.Imaging.EncoderParameters
$ep.Param[0] = New-Object Drawing.Imaging.EncoderParameter ([System.Drawing.Imaging.Encoder]::Quality, [long]100)
$screenCapturePathBase = "$pwd\ScreenCapture"
$c = 0
while (Test-Path "${screenCapturePathBase}${c}.jpg") {
$c++
}
$bitmap.Save("${screenCapturePathBase}${c}.jpg", $jpegCodec, $ep)
}
}
-
Are there additional steps needed to get this to work on Windows 7 ? I'm getting the following error when running the function:"Unable to find type [Windows.Forms.Sendkeys]: make sure that the assembly containing this type is loaded." and then a further error "Unable to find type [Windows.Forms.Clipboard]: make sure that the assembly containing this type is loaded." , and then one more error (but I think because the previous two calls failed). Mar 21, 2014 at 17:06
-
1You probably haven't loaded Winforms. I'd recommend you simply download the module this answer is now in: RoughDraft Mar 24, 2014 at 22:29
-
For future readers, adding
Add-Type -assembly System.Windows.Forms
before the function fixes the issue mentioned by @monojohnny Jul 6 at 20:17
Here is my solution for multi-monitor, which is a bit simpler than the current answer. It also will render screens properly if the user has a strange monitor config (stacked vertical, etc) without black bars.
Add-Type -AssemblyName System.Windows.Forms,System.Drawing
$screens = [Windows.Forms.Screen]::AllScreens
$top = ($screens.Bounds.Top | Measure-Object -Minimum).Minimum
$left = ($screens.Bounds.Left | Measure-Object -Minimum).Minimum
$right = ($screens.Bounds.Right | Measure-Object -Maximum).Maximum
$bottom = ($screens.Bounds.Bottom | Measure-Object -Maximum).Maximum
$bounds = [Drawing.Rectangle]::FromLTRB($left, $top, $right, $bottom)
$bmp = New-Object System.Drawing.Bitmap ([int]$bounds.width), ([int]$bounds.height)
$graphics = [Drawing.Graphics]::FromImage($bmp)
$graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size)
$bmp.Save("$env:USERPROFILE\test.png")
$graphics.Dispose()
$bmp.Dispose()
-
-
The currently most upvoted answer for multi-monitor: stackoverflow.com/a/44609221/4868262 Jun 7, 2022 at 13:45
Microsoft have a PowerShell script available here:
http://gallery.technet.microsoft.com/scriptcenter/eeff544a-f690-4f6b-a586-11eea6fc5eb8
I have just tried it on a Windows 7 machine and it to work, using the commandline example provided:
Take-ScreenShot -screen -file "C:\image.png" -imagetype png
-
Where do you place this .ps1 file so you don't have to type the entire network path in ?– AaronAug 27, 2014 at 13:40
-
@Aaron, not quite sure how best to do this - but probably set it in Powershell profile (perhaps either by editing PATH variable, or using an alias) : this article may help :stackoverflow.com/questions/714877/… Sep 2, 2014 at 15:07
-
2Open with... produces no outcome. Input in console then invoking it produces no input either. This is Windows 10 and this thread has been here for years without producing input. Feb 23, 2017 at 22:49
-
2The link is (effectively) broken. It redirects to the unspecific URL
https://learn.microsoft.com/en-us/samples/browse/?redirectedfrom=TechNet-Gallery
. May 13, 2022 at 23:27