Take a Screenshot of a User’s Desktop with PowerShell

The HelpDesk support team asked me to write a PowerShell script to quickly get a screenshot of a user’s desktop from a remote computer. The most important condition is that the HelpDesk employee may not connect to the user’s computer via graphical remote support tools (SCCM, Remote Assistance, Remote Desktop Session Shadowing, etc.).

Contents:

  • Screenshot with PowerShell
  • How do I take a screenshot of my desktop from a remote computer using PowerShell?

Screenshot with PowerShell

First, let’s learn how to take screenshots on the local computer using PowerShell. You can use the built-in .NET System.Windows.Forms class forms to capture the current desktop image. I have a PowerShell script:

$Path = C:ScreenCapture
# Make sure the folder for saving screenshots is created, otherwise create a
If (!(test-pad $path))
New-Item -ItemType Directory -Force -Path $path
}
Add-Type -AssemblyName System.Windows.Forms
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
# Get the current screen resolution
$image = New-Object System.Drawing.Bitmap($screen.Width, $screen.Height)
# Create a graphics object
$graphic = [System.Drawing.Graphics]::FromImage($image)
$point = New-Object System.Drawing.point(0, 0)
$graphic.CopyFromScreen($point, $point, $image.Size);
$cursorBounds = New-Object System.Drawing.Rectangle([System.Windows.Forms.Cursor]::Position, [System.Windows.Forms.Cursor]::Current.Size)
# Screenshot
[System.Windows.Forms.Cursors]::Default.Draw($graphic, $cursorBounds)
$scscreen_file = $Path + $env:computername + _ + $env:username + _ + $((get-date).tostring(‘yyyyy.MM.dd-H.mm.ss)))png
# Save screenshot as PNG file
$image.Save($screen_file, [System.Drawing.Imaging.ImageFormat]::Png].

This PowerShell script creates a folder to store screenshots, fetches the current screen resolution, captures the image of the current workspace, and saves it as a PNG file. Run the PowerShell script and make sure a png file (you can specify the UNC path to a shared network folder) is displayed with a screenshot of the desktop in the specified folder. For your convenience, the PNG file name contains the computer name, user name, current date and time.

If you want to invoke the PS script from a batch file, use this command (in this case you do not need to change the PowerShell output policy settings) :

powershell.exe – execution policy bypass – profile c:psCaptureLocalScreen.ps1

You can create a GPO to place a shortcut for the PowerShell script on the desktop of all users in the domain and assign shortcuts to it. Now, when a problem or error occurs in an application, the user can simply press the shortcut keys assigned to him. A screenshot of the user’s desktop then appears in the shared HelpDesk folder.

How do I take a screenshot of the desktop of a remote computer using PowerShell?

The next task is to obtain a screenshot of the user’s desktop on the remote computer via PowerShell. It can be a stand-alone computer with Windows 10 or an RDS server.

The preferred method of connecting to the user’s desktop on the RDS server using a graphical tool is the RDP shadow session.

If you want to get a screenshot of an RDS server desktop (or a Windows desktop that allows multiple simultaneous RDP connections), you first need to determine the user session ID on the remote computer. Enter the name of the remote computer/server and the user account in the following PowerShell script:
$ComputerName = nld-rds1
$RDUserName = h.jans
$quser = (((query user /server:$ComputerName) – replaces ‘^>’, ”) – replaces ‘s{2,}’, ‘,’ | ConvertFrom-Csv)
$usersess=$quser | where {$_.USERNAME – such as $RDUserName -and $_.STATE -eq Active}
$usersessID=$usersess.ID

If you use the script to retrieve screenshots on remote computers with only one user, the session number will always be 1. Replace the previous request block of the RDS server with $usersessID = 1.

For your convenience, save the PowerShell script file in a shared network folder. Then edit the CaptureLocalScreen.ps1 file and change the path to it:

$path = \nld-fs01ScreenLog

The user’s screenshots are saved in this folder. Give write permissions to the folder for the domain group of authenticated users.

Once you have the user session ID, you can use the PsExec tool to remotely connect to the user session and run the script:

PsExec.exe -s -i $usersessID $ComputerName powershell.exe -Bypass Execution Policy -WindowStyle Hidden -file \nld-fs01ScreenCaptureLocalScreen.ps1

The helpdesk team member can then run the script from his or her computer and a screenshot of the current desktop on the remote computer is displayed in the specified folder.

Related Tags:

powershell snipping tool,script to take screenshot and email,remote screenshot capture,powershell screenshot window,screenshots powershell,powershell take snapshot,powershell script to screenshot,batch file to take screenshot and save,powershell get-screenshot,take screenshot of remote computer powershell,powershell run script on remote computer,powershell start-process on remote computer,get-childitem remote computer,powershell invoke script,powershell run batch file on remote computer,script to take screenshot