Skip to content

Instantly share code, notes, and snippets.

@csdy
Created May 3, 2024 21:00
Show Gist options
  • Save csdy/915a4734a310ab0d2054f984f8140c65 to your computer and use it in GitHub Desktop.
Save csdy/915a4734a310ab0d2054f984f8140c65 to your computer and use it in GitHub Desktop.
Capture Screenshot of All Monitors
[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++) {
# Initialize screenshot
$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"
# Capture screenshot
CaptureScreenshot $Bounds $FileName
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment