Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / PowerShell / Take a Screenshot of a User’s Desktop with PowerShell

May 10, 2023 PowerShellWindows 10Windows Server 2016

Take a Screenshot of a User’s Desktop with PowerShell

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

Contents:
  • Capturing Screenshots Using PowerShell
  • How to Take a Desktop Screenshot from a Remote Computer Using PowerShell?

Capturing Screenshots Using PowerShell

First of all, let’s learn how to take a screenshot on a local computer with PowerShell. To capture a current desktop image, you can use the built-in .NET class System.Windows.Forms. I got this PowerShell script:

$Path = "C:\ScreenCapture"
# Make sure that the directory to keep screenshots has been created, otherwise create it
If (!(test-path $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 graphic 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)
# Get a screenshot
[System.Windows.Forms.Cursors]::Default.Draw($graphic, $cursorBounds)
$screen_file = "$Path\" + $env:computername + "_" + $env:username + "_" + "$((get-date).tostring('yyyy.MM.dd-HH.mm.ss')).png"
# Save the screenshot as a PNG file
$image.Save($screen_file, [System.Drawing.Imaging.ImageFormat]::Png)

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

If you want to call the PS script from a batch file, use this command (in this case, you don’t need to change the PowerShell Execution Policy settings):

powershell.exe -executionpolicy bypass -file c:\ps\CaptureLocalScreen.ps1

CaptureLocalScreen - powershell screen take desktop screenshot

To edit PowerShell scripts, I prefer using Visual Studio Code instead of Powershell ISE.

You can create a GPO to place a shortcut for the PowerShell script on the desktops of all domain users and assign hot keys to call it. Now, when a problem or error appears in any app, a user can just press the assigned hot keys. Then a user desktop screenshot appears in the HelpDesk shared folder.

How to Take a Desktop Screenshot from a Remote Computer Using PowerShell?

The next task is to get a screenshot of the user’s desktop on the remote computer via PowerShell. It may be either a standalone computer running Windows 10 or an RDS server.

A preferred way to connect to a user desktop on an RDS server using a graphic tool is the Shadow RDP Session.

If you want to get a desktop screenshot from an RDS server (or a desktop Windows, in which multiple concurrent RDP connections are allowed), you must first get a user session ID on the remote computer. Specify the name of a remote computer/server and a user account in the following PowerShell script:
$ComputerName = "nld-rds1"
$RDUserName = "h.jansen"
$quser = (((query user /server:$ComputerName) -replace '^>', '') -replace '\s{2,}', ',' | ConvertFrom-Csv)
$usersess=$quser | where {$_.USERNAME -like $RDUserName -and $_.STATE -eq "Active"}
$usersessID=$usersess.ID

If you use the script to get screenshots from remote computers with a single user, the session number will always be 1. Replace the previous RDS server query block with $usersessID = 1.

To make it more convenient, save the PowerShell script file to a shared network folder. Then edit the CaptureLocalScreen.ps1 file and change the path to:

$Path = \\nld-fs01\Screen\Log

User screenshots will be saved to this folder. Grant write permissions to the folder for the Authenticated Users domain group.

After you have got a user session ID, you can connect to the user session remotely using PsExec tool and run the script:

.\PsExec.exe -s -i $usersessID \\$ComputerName powershell.exe -executionpolicy bypass -WindowStyle Hidden -file "\\nld-fs01\Screen\CaptureLocalScreen.ps1"

Then a HelpDesk team member can run the script from his computer, and a screenshot of the current desktop of the remote computer will appear in the specified directory.

4 comments
2
Facebook Twitter Google + Pinterest
previous post
Using the Unified Write Filter (UWF) on Windows 10
next post
How to Extend or Shrink Virtual Hard Disks on Hyper-V?

Related Reading

How to Connect VPN Before Windows Logon

November 14, 2023

Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

November 7, 2023

Send Emails with Microsoft Graph API and PowerShell

November 6, 2023

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

4 comments

elghazrani abdelali December 14, 2020 - 9:42 pm

the script is really hellpfull

Reply
Madhu June 28, 2022 - 1:39 am

This captures the entire screen. Is there way to capture only a application window.

Requirements
1. Need to capture only a specific program window that is open.
2. What if there are multiple screens to the computer.

Reply
Daniel January 11, 2023 - 8:19 am

Thank you for sharing this useful script.
My problem is that taken screenshot is not correct (not full screen shown) if in display settings “Scale” set to 125%.
Please suggest solution.
Thank you

Reply
SHAHZAD MAHMOOD February 7, 2023 - 3:48 pm

Works great from the command line.
I am invoking from the windows task scheduler via a bat file and the image captured is clear having no information. Any clue

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • How to Connect VPN Before Windows Logon

    November 14, 2023
  • Removing Azure Arc Setup Feature on Windows Server 2022

    November 9, 2023
  • Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

    November 7, 2023
  • Send Emails with Microsoft Graph API and PowerShell

    November 6, 2023
  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • How to Hide Installed Programs in Windows 10 and 11
  • PowerShell: Get Folder Sizes on Disk in Windows
  • Managing Printers and Drivers with PowerShell in Windows 10 / Server 2016
  • Deploy PowerShell Active Directory Module without Installing RSAT
  • Managing User Photos in Active Directory Using ThumbnailPhoto Attribute
  • Managing Saved Passwords Using Windows Credential Manager
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top