Skip to content

Instantly share code, notes, and snippets.

@exorcistas
Created April 23, 2022 19:50
Show Gist options
  • Save exorcistas/343b1392cafdcc255cb4c86f1b2f446d to your computer and use it in GitHub Desktop.
Save exorcistas/343b1392cafdcc255cb4c86f1b2f446d to your computer and use it in GitHub Desktop.
Powershell script to capture screenshot
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
$width = ($screens.Bounds.Right | Measure-Object -Maximum).Maximum
$height = ($screens.Bounds.Bottom | Measure-Object -Maximum).Maximum
$bounds = [Drawing.Rectangle]::FromLTRB($left, $top, $width, $height)
$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)
$timestamp = Get-Date -Format "yyyy-MM-dd HH_mm_ss_fff"
$filepath = "$env:USERPROFILE\Desktop\Screenshot_$timestamp.png"
$bmp.Save($filepath)
$graphics.Dispose()
$bmp.Dispose()
Write-Host $filepath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment