Want to capture screenshot of multiple system using powershell. But don't know how

SURAJ KUMAR 41 Reputation points
2022-11-07T04:27:10.317+00:00

I have multiple system and every system having different display resolution. Minumum display resolution is 1024x768 and Maximum display resolution is 1920x1080. Don't know how to make this code auto adjust according to display resolution.

[Reflection.Assembly]::LoadWithPartialName("System.Drawing")  
function screenshot([Drawing.Rectangle]$bounds, $path) {  
   $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($path)  
  
   $graphics.Dispose()  
   $bmp.Dispose()  
}  
  
#NUC bounds  
$bounds = [Drawing.Rectangle]::FromLTRB(0, -1080, 1920, 1080)  
  
$PC_name=$(Get-WmiObject Win32_Computersystem).name  
  
$dateandtime = Get-Date -Format yyyy-MM-dd-hh-mm-ss  
  
$path_pcname= "C:\Scripts\Screenshots\" + $PC_name + "_screenshot_" + "$dateandtime"+ ".png"  
  
  
screenshot $bounds $path_pcname  
  
$limit = (Get-Date).AddMinutes(-15)  
$path_todelete = "C:\Scripts\Screenshots\"  
  
# Delete files older than the $limit.  
Get-ChildItem -Path $path_todelete -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force  
  
Start-Sleep -Seconds 10  
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. DaveK 1,871 Reputation points
    2022-11-07T08:20:15.557+00:00

    Hi, you could maybe try something like this to return the current screen resolution using WMI.

    $CurrentRes = Get-WmiObject -Class Win32_VideoController -Property CurrentVerticalResolution,CurrentHorizontalResolution  
    $bounds = [Drawing.Rectangle]::FromLTRB(0, 0,  $CurrentRes.CurrentHorizontalResolution, $CurrentRes.CurrentVerticalResolution)  
    

    It might need to be adjusted if you need to capture over multiple screens so it really depends on what your desired outcome is, on my system this works to grab my main display at 1440p without manually inputting the screen resolution. If taking the whole screen area you maybe able to use:

    [System.Windows.Forms.Screen]::AllScreens  
    

    Taking the output to calculate the whole area covered by all screens and grab the whole area.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.