Save-ScreenCapture.ps1

<#PSScriptInfo
 
.VERSION 1.0.0.0
 
.GUID f5783ffa-47a5-49ab-ad1b-d2624d662fd7
 
.AUTHOR Jeffrey Snover
 
.COMPANYNAME Microsoft
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Capture and save one or more screens to a file as a .JPG.
 You provide a directory or it will use the current directory.
 The files will be named by combining the screen device (e.g. DISPLAY1) and a timestamp.
 Use -Verbose to see the resultant filename.
    Note - this is based upon a code fragment from a piece of malware discovered by John Lambert @JohnLATwC
    It looked pretty useful so I decided to clean it up and make it available to the rest of the world.
.SYNOPSIS
 Capture and save one or more screens to a file as a .JPG.
 
#>
 
Param(
[Parameter()]
[Alias("Path")]
[string]$Directory = ".",

[Parameter()]
[Switch]$AllScreens
)
            
            
Set-StrictMode -Version 2
Add-Type -AssemblyName System.Windows.Forms

if ($AllScreens)
{
    $Capture = [System.Windows.Forms.Screen]::AllScreens 
}
else
{
    $Capture = [System.Windows.Forms.Screen]::PrimaryScreen
}
foreach ($C in $Capture)
{
    $FileName = '{0}-{1}.jpg' -f (Join-Path (Resolve-Path $Directory) ($c.DeviceName -split "\\")[3]),(Get-Date).ToString('yyyyMMdd_HHmmss')
            
            
    $Bitmap = New-Object System.Drawing.Bitmap($C.Bounds.Width, $C.Bounds.Height)
    $G = [System.Drawing.Graphics]::FromImage($Bitmap)
    $G.CopyFromScreen($C.Bounds.Location, (New-Object System.Drawing.Point(0,0)), $C.Bounds.Size)
    $g.Dispose()

    $Quality = [System.Drawing.Imaging.Encoder]::Quality
    $EncoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1) 
    $EncoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($Quality, 20) 
    $JPGCodec = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | Where{$_.MimeType -eq 'image/jpeg'}
    Write-Verbose ("Saving ScreenCapture $FileName")
    $Bitmap.Save($FileName ,$JPGCodec, $EncoderParams)
}