Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • 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 2012
    • 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

December 8, 2020 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.

1 comment
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

Create Organizational Units (OU) Structure in Active Directory...

May 17, 2022

Windows Security Won’t Open or Shows a Blank...

May 17, 2022

How to Manually Install Windows Updates from CAB...

May 16, 2022

RDS and RemoteApp Performance Issues on Windows Server...

May 16, 2022

Deploying Software (MSI Packages) Using Group Policy

May 12, 2022

1 comment

elghazrani abdelali December 14, 2020 - 9:42 pm

the script is really hellpfull

Reply

Leave a Comment Cancel Reply

Categories

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

Recent Posts

  • Create Organizational Units (OU) Structure in Active Directory with PowerShell

    May 17, 2022
  • Windows Security Won’t Open or Shows a Blank Screen on Windows 10/ 11

    May 17, 2022
  • How to Manually Install Windows Updates from CAB and MSU Files?

    May 16, 2022
  • RDS and RemoteApp Performance Issues on Windows Server 2019/2016

    May 16, 2022
  • Deploying Software (MSI Packages) Using Group Policy

    May 12, 2022
  • Updating VMware ESXi Host from the Command Line

    May 11, 2022
  • Enable or Disable MFA for Users in Azure/Microsoft 365

    April 27, 2022
  • Fix: You’ll Need a New App to Open This Windows Defender Link

    April 27, 2022
  • How to Reset an Active Directory User Password with PowerShell and ADUC?

    April 27, 2022
  • How to Completely Uninstall Previous Versions of Office with Removal Scripts?

    April 26, 2022

Follow us

woshub.com

ad

  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Installing RSAT Administration Tools on Windows 10 and 11
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • How to Hide Installed Programs in Windows 10 and 11?
  • How to Find the Source of Account Lockouts in Active Directory domain?
  • Get-ADComputer: Find Computer Details in Active Directory with PowerShell
  • How to Create a UEFI Bootable USB Drive to Install Windows 10 or 7?
  • Adding Third-Party Drivers into VMWare ESXi 6.7 ISO Image
Footer Logo

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


Back To Top