0

I have a request to get a screenshot of users desktop icons. I can use Powershell to capture, but need to clear the screen first, take the shot, then restore the screen. Keystrokes 'Windows+D" will do it, but the Windows key is not an option to simulate in Powershell. Is there another way to capture a screenshot of the desktop?

Thanks much!

2 Answers 2

2

here's a solution that also takes the screenshot. I'm using that in my scripts where I need to screenshot something. why automate only parts of the task, when you're also able to automate everything ;-) right?

# Take Screenshot function - reads width and height from WMI, saves in outfile path
function Take-Screenshot([string]$outfile) 
{
    [int]$PrtScrnWidth = (gwmi Win32_VideoController).CurrentHorizontalResolution
    [int]$PrtScrnHeight = (gwmi Win32_VideoController).CurrentVerticalResolution
    $bounds = [Drawing.Rectangle]::FromLTRB(0, 0, $PrtScrnWidth, $PrtScrnHeight)
    $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($outfile)
    $graphics.Dispose()
    $bmp.Dispose()
}

# Minimize all the Windows
$shell = New-Object -ComObject "Shell.Application"
$shell.minimizeall()

#sleep to make sure not to screenshot while everything is still minimizing
sleep -s 2

# Take the Screenshot - choose your outfile path
Take-Screenshot -outfile C:\Batch\test4.png

# get your screen back
$shell.undominimizeall()
4
  • That's brilliant! Thanks, it's the 'minimize all windows' that I needed. I don't see how you found that, no way to trace what the 'Windows+D" actually calls. Thanks so much!!
    – JCauble
    Jun 12, 2016 at 17:06
  • SimonS, if you're still there, how can I change the line "Take-Screenshot -outfile C:\Batch\test4.png" to a variable? Everything I've done so far result in an error at the "$bmp.Save($outfile)" line, stating ""The path is not of a legal form"". Depending on where it is run from, I need to change the path. Thanks again!
    – JCauble
    Jun 14, 2016 at 1:46
  • @JCauble you could do something like this $outfile = Z:\somewhere\somewhat.png and then Take-Screenshot -outfile $outfile
    – SimonS
    Jun 14, 2016 at 7:25
  • @JCauble if you mean the name of the screenshot itself should be variable, you could add a DateTime or $env:computername like this $DateTime = (get-date).tostring('yyyyMMdd-HHmmss') and then Take-Screenshot -outfile "Z:\somewhere\somewhat-$DateTime-$env:computername.png"
    – SimonS
    Jun 14, 2016 at 8:46
1

There is example here .

I came across this little tip while exploring shell.application com object. It has other useful functions like undominimizeall, cascade windows, and many other explorer functions.

$shell = New-Object -ComObject "Shell.Application"
$shell.minimizeall()

You can also undo minimize all windows by using below code.

$shell = New-Object -ComObject "Shell.Application"
$shell.undominimizeall()
2
  • SimonS, if you're still there, how can I change the line "Take-Screenshot -outfile C:\Batch\test4.png" to a variable? Everything I've done so far result in an error at the "$bmp.Save($outfile)" line, stating ""The path is not of a legal form"". Depending on where it is run from, I need to change the path. Thanks again!
    – JCauble
    Jun 13, 2016 at 22:48
  • YES! That's exactly what I needed! I don't know why I couldn't get it to work before, but I am learning, and really appreciate these tips.
    – JCauble
    Jun 15, 2016 at 17:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .