Skip to main content

How to capture a screenshot with PowerShell and .NET.

#############################################################################
# Taking a screenshot with PowerShell and .NET
# http://www.adminarsenal.com/admin-arsenal-blog/capturing-screenshots-with-powershell-and-.net/
#############################################################################

Add-Type -AssemblyName System.Windows.Forms
Add-type -AssemblyName System.Drawing

# Path to this script's directory
$scriptDir = $PSScriptRoot

# Path for saved screenshot (with timestamp appended)
$screenshotPath = "$scriptDir\screenshot_" + (Get-Date -Format "MM-dd-yyyy_hhmmss") + ".png"

# Gather Screen resolution information
$screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
$width = $screen.Width
$height = $screen.Height
$left = $screen.Left
$top = $screen.Top

# Create bitmap using the top-left and bottom-right bounds
$bitmap = New-Object System.Drawing.Bitmap $width, $height

# Create Graphics object
$graphic = [System.Drawing.Graphics]::FromImage($bitmap)

# Capture screen
$graphic.CopyFromScreen($left, $top, 0, 0, $bitmap.Size)

# Save to file
$bitmap.Save($screenshotPath)

Write-Output "Screenshot saved to:"
Write-Output $screenshotPath