Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / PowerShell / Taking User Desktop Screenshots with PowerShell

January 31, 2025 PowerShellWindows 11Windows Server 2022

Taking User Desktop Screenshots with PowerShell

The HelpDesk support team requested a PowerShell script to quickly capture a screenshot of a user’s desktop from a remote computer. The main requirement is that the HelpDesk employee may not be connected to the user’s computer using graphical remote support tools (SCCM, Remote Assistance, Remote Desktop Session Shadowing, etc.).

Contents:
  • Capturing Desktop Screenshots Using PowerShell
  • How to Take a Desktop Screenshot from a Remote User Session
  • PowerShell: Take a Screenshots of Active Window Only

Capturing Desktop Screenshots Using PowerShell

Use the built-in .NET System.Windows.Forms class to get the user’s current screen. The following PowerShell script creates a local folder to store screenshot files, checks the current resolution of the graphical environment, and saves the screen contents to a PNG file.

$Path = "C:\ScreenCapture"
# Make sure the directory for saving 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 = [Windows.Forms.SystemInformation]::VirtualScreen
# Get 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)
# Take 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)
# Freeing up memory
$graphic.Dispose()
$image.Dispose()

Check out our GitHub repo for the full CaptureScreenshot.ps1 script code.

Run the PowerShell script and verify that a PNG file that contains an image of the current screen workspace has appeared in the specified directory (here you can specify the UNC path to the shared network folder). For convenience, the PNG file name includes a computer name, a user name, and the current date and time.

powershell script to take screenshot

The script will also capture the mouse pointer position and show it in the screenshot.

If multiple monitors are connected to a computer, the screenshot will include all desktops on the active monitors (not just the primary monitor).

To capture and save desktop screenshots every few seconds, you need to put the script code in a While loop:
while($true){
{ SCRIPT }
sleep(60) # 60 sec pause
}

To run this PowerShell script from the Task Scheduler or a BAT script file, use the following command (in this case, you don’t need to change the PowerShell execution policy settings):

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

You can use GPO to deploy a shortcut to this PowerShell script on users’ desktops and assign hotkeys to run it. Now, when an issue or error occurs in any app, the user can simply press the assigned hotkeys to capture a desktop screen and save it as a PNG file in the HelpDesk shared folder.

How to Take a Desktop Screenshot from a Remote User Session

This PowerShell script can be used to take a screenshot of a user’s desktop on a remote workstation or server.

To capture a desktop screenshot from a user session on an RDS host (or a Windows desktop computer that allows multiple concurrent RDP connections), first find the user session ID on the remote computer.

Run the command to list the logged user sessions on a remote computer:

qwinsta /server:nld-rds1

qwinsta: list user sessions on remote computer

A Windows Firewall rule that allows access on port 445 must be enabled on the remote computer (rule name File and Printer Sharing (SMB-In)).  This firewall rule can be enabled via GPO.

The user is logged in through the computer console (session 1) in this case

Copy the CaptureScreenshot.ps1 file to a folder on a file server. Edit the path in the file so that it saves screenshots to a shared folder. Grant the Authenticated Users or the Domain Users group write permissions to this shared folder.

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

You can now use the PsExec tool to run a PowerShell script to get a screenshot of a remote user’s session by their session ID ( in this example, it is ID 1 for the console/local session).

.\PsExec.exe -s -i 1 \\wks321 powershell.exe -executionpolicy bypass -WindowStyle Hidden -file "\\nld-fs01\scripts\CaptureScreenshot.ps1"

psexec: take screenshot from remote computer

The line ‘powershell.exe exited on CompName with error code 0‘ indicates that the script ran successfully.

Now, HelpDesk team members can run this command from their computer, and a screenshot of the remote computer user’s desktop will appear in the specified shared folder.

PowerShell: Take a Screenshots of Active Window Only

The PS1 script described above takes a screenshot of all user monitors and saves it to a PNG file. You can only take a screenshot of the open (active) app window that the user is currently working in.

An updated CaptureScreenshotActiveWindow.ps1 script has also been uploaded to GitHub. It allows taking a screenshot of only the contents of the active program window in a user’s session.

5 comments
8
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

Configure NTP Time Source for Active Directory Domain

May 6, 2025

How to Cancel Windows Update Pending Restart Loop

May 6, 2025

View Windows Update History with PowerShell (CMD)

April 30, 2025

Cannot Install Network Adapter Drivers on Windows Server

April 29, 2025

Change BIOS from Legacy to UEFI without Reinstalling...

April 21, 2025

5 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
MP October 25, 2024 - 7:33 pm

Hello,

i ran this in my local network it works but only if the script is on local PC and print screen is saved in local PC

I cannot run script from a network share or even save printscreen to a network share

C:\PsExec.exe -s -i 1 \\192.168.1.246 powershell.exe -executionpolicy bypass -WindowStyle Hidden -file “\\192.168.1.231\SCREEN\screenshot2.ps1”

and in print script $Path = “\\192.168.1.231\SCREEN”

sharing security on folder its done on everyone

Reply

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

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

  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025
  • How to Write Logs to the Windows Event Viewer from PowerShell/CMD

    March 3, 2025
  • How to Hide (Block) a Specific Windows Update

    February 25, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • How to Download Offline Installer (APPX/MSIX) for Microsoft Store App
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • How to Hide Installed Programs in Windows 10 and 11
  • Using Credential Manager on Windows: Ultimate Guide
  • Managing Printers and Drivers on Windows with PowerShell
  • PowerShell: Get Folder Size on Windows
  • Protecting Remote Desktop (RDP) Host from Brute Force Attacks
Footer Logo

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


Back To Top