Home > Powershell > Function: Get-Screenshot

Function: Get-Screenshot

Function: Get-Screenshot

Just as I was leaving for the day… From across the room came a granade:

I wish there was a way to grab a screenshot from the command line

Somewhere in the back of my mind I have a vague memory of catching this in a mail / post, and true enough, techtalkz.com had the sample I was after. I messed with it a few minutes, and refined it adding my usual variables. Then stripping out the taskbar (thanks to Shay Levi) and getting the screen size dynamically (thanks Keith Hill). The result is the Function below.

Function Get-Screenshot ($filename="PrintScreen",$ext="png",$fold="C:\temp",[switch]$DISPLAY,[switch]$FULL)
	$date = Get-Date -Format yyyyMMdd_HHmm
	if (!(Test-Path $fold)) {md $fold}
	$repfile = "$fold\$filename`_$date.$ext"
	if ($FULL) 
		$screen = gwmi Win32_DesktopMonitor
		[int32]$height = $screen[0].screenHeight
		[int32]$width = $screen[0].screenWidth
	} else {
		$workarea = [System.Reflection.Assembly]::LoadWithPartialName('PresentationFramework')
		[int32]$height = [System.Windows.SystemParameters]::FullPrimaryScreenHeight
		[int32]$width = [System.Windows.SystemParameters]::FullPrimaryScreenWidth
	[reflection.assembly]::LoadWithPartialName("System.Drawing") > $null
	$Bitmap = New-Object System.Drawing.Bitmap $width,$height
	$Size = New-object System.Drawing.Size $width,$height
	$FromImage = [System.Drawing.Graphics]::FromImage($Bitmap)
	$FromImage.copyfromscreen(0,0,0,0, $Size,([System.Drawing.CopyPixelOperation]::SourceCopy))
	$Bitmap.Save($repfile,([system.drawing.imaging.imageformat]::png)); # the acceptable values: png, jpeg, bmp, gif...
	if ($DISPLAY) {
		Invoke-Item $repfile
	} else {
		return "PrintScreen saved to: $repfile"


  • The $DISPLAY switch is a simple way to flag the output to screen
  • The mix of WMI and .NET is here to show that both can be used
  • There’s a few ideas spawned off from here, tbc

    Categories: Powershell
    1. No comments yet.
    1. March 18, 2011 at 11:39 am

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out /  Change )

    Google photo

    You are commenting using your Google account. Log Out /  Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out /  Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out /  Change )

    Connecting to %s

    %d bloggers like this: