Take Screenshots using all Active Monitors

Code Properties

  • Language: PowerShell
  • Assemblies: System.Drawing, System.Windows.Forms

Overview

Sources:

PowerShell script that captures screenshots from all connected monitors and saves them to a specified folder with timestamps.

Code

[Reflection.Assembly]::LoadWithPartialName("System.Drawing")
 
function CaptureScreenshot([Drawing.Rectangle]$Bounds, $Path) {
    # initialize image
    $Image    = New-Object Drawing.Bitmap $Bounds.Width, $Bounds.Height
    $Graphics = [Drawing.Graphics]::FromImage($Image)
    $Graphics.CopyFromScreen($Bounds.Location, [Drawing.Point]::Empty, $Bounds.Size)
 
    # save screenshot
    $Image.Save($Path)
 
    # cleanup
    $Graphics.Dispose()
    $Image.Dispose()
}
 
# initialize path
$BasePath = "C:\screenshots"
if (-not (Test-Path -Path $BasePath -PathType Container)) {
    New-Item -Path $BasePath -ItemType Directory -Force
}
 
# get monitors
Add-Type -AssemblyName System.Windows.Forms
$Screens = [System.Windows.Forms.Screen]::AllScreens
 
# capture monitors
$DateTime = Get-Date -Format yyyyMMddHHmmss
for ($i = 0; $i -lt $Screens.Length; $i++) {
    $Screen   = $Screens[$i]
    $Left     = $Screen.Bounds.X
    $Top      = $Screen.Bounds.Y
    $Right    = $Screen.Bounds.X + $Screen.Bounds.Width
    $Bottom   = $Screen.Bounds.Y + $Screen.Bounds.Height
    $Bounds   = [Drawing.Rectangle]::FromLTRB($Left, $Top, $Right, $Bottom)
    $FileName = "${BasePath}\${DateTime}_${i}.png"
    
    CaptureScreenshot $Bounds $FileName
}

Usage

# run script to capture all monitors
.\Capture-AllMonitors.ps1
 
# screenshots saved to C:\screenshots\ with timestamp prefixes

Appendix

Note created on 2024-05-03 and last modified on 2024-12-31.

See Also


(c) No Clocks, LLC | 2024