Skip to content

Instantly share code, notes, and snippets.

@michaelvdnest
Created May 22, 2015 09:11
Show Gist options
  • Save michaelvdnest/2679bdd8d65ab9277a7c to your computer and use it in GitHub Desktop.
Save michaelvdnest/2679bdd8d65ab9277a7c to your computer and use it in GitHub Desktop.
Saves a screenshot of the current desktop to a file.
<#
.SYNOPSIS
Saves a screenshot of the current desktop to a file.
.DESCRIPTION
Saves a screenshot of the current desktop to a file. Supported extensions for the
image file are BMP, GIF, EXIF, JPG, PNG and TIFF.
.PARAMETER Path
Mandatory. Specifies the path to the file that will be created.
.NOTES
Version: 1.0
Author: Michael van der Nest
Creation Date: 2013/07/25
Purpose/Change: Initial script development
.EXAMPLE
Export-Screenshot -File 'H:\screenshot.png'
#>
[Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null
Function Export-Screenshot() {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)][string]$Path,
[Parameter(Mandatory=$false)][string]$LogFile
)
Write-Verbose 'Performing operation "Export-Screenshot"'
if ($LogFile) {
Log-Write -LogPath $sLogFile -LineValue 'Performing operation "Export-Screenshot"'
}
Get-WmiObject -Class Win32_DesktopMonitor |
% {
$desktop = $_
$bounds = [Drawing.Rectangle]::FromLTRB(0, 0, $desktop.ScreenWidth, $desktop.ScreenHeight)
Write-Verbose "Desktop width $($bounds.Width), height $($bounds.Height)"
if ($LogFile) {
Log-Write -LogPath $sLogFile -LineValue "Desktop width $($bounds.Width), height $($bounds.Height)"
}
$bmp = New-Object Drawing.Bitmap $bounds.Width, $bounds.Height
$graphics = [Drawing.Graphics]::FromImage($bmp)
$graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.Size)
$bmp.Save($Path)
Write-Verbose "Screenshot saved to $Path"
if ($LogFile) {
Log-Write -LogPath $sLogFile -LineValue "Screenshot saved to $Path"
}
$graphics.Dispose()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment