2

Don't want to use third party tool and module.

$ie = New-Object -ComObject InternetExplorer.Application 
$ie.Visible = $true 
$ie.Navigate("https://www.google.com") 
$ie.FullScreen = $true 
while($ie.busy){ 
Start-Sleep -Seconds 10 
} 
Add-Type -AssemblyName System.Drawing 
$bitmap = New-Object -TypeName System.Drawing.Bitmap -ArgumentList $ie.Width, $ie.Height 
$graphics = [System.Drawing.Graphics]::FromImage($bitmap) 
$graphics.CopyFromScreen($ie.Left, $ie.Top, 0, 0, $bitmap.Size) 
$ie.Quit() 
$bitmap.Save("$env:HOMEPATH\desktop\capt.png")
0

1 Answer 1

3

Why?

There is little reason to try and script this.

MS Edge on Windows 10 and higher already has a screen capture feature. You just need to enable it. It's called 'Web Capture '.

https://techcommunity.microsoft.com/t5/articles/introducing-web-capture-for-microsoft-edge/m-p/1721318

Note, if you are just trying to capture stuff on-screen real-time for documentation, then look at the built-in Windows OS Problem Step Recorder(PSR) tool.

https://learn.microsoft.com/en-us/office/troubleshoot/settings/how-to-use-problem-steps-recorder

It's been in Windows for decades.

Lastly, there are already modules in the mspowershellgallery.com that can also provide screen capture.

Find-Module -Name '*screenshot*'

Version Name                                 Repository Description
------- ----                                 ---------- -----------                                                                                                                     
1.0     PSScreenshot                         PSGallery  Save a screenshot from PowerShell.
6.0.0   Invoke-ExportServerScreenShotREDFISH PSGallery  iDRAC cmdlet using Redfish API with ...

If you absolutely want to script this manually, you can do something like this. Though this is not MSEdge specific, this can be used for whatever, as it's for the whole primary screen.

Add-Type -AssemblyName System.Windows.Forms,
                       System.Drawing 
Start-Process -FilePath 'msedge' -ArgumentList '-inprivate', 'https://www.google.com' -Wait
$screens = [Windows.Forms.Screen]::PrimaryScreen
$top     = ($screens.Bounds.Top    | 
           Measure-Object -Minimum).Minimum

$left   = ($screens.Bounds.Left   | 
          Measure-Object -Minimum).Minimum

$width  = ($screens.Bounds.Right  | 
          Measure-Object -Maximum).Maximum

$height = ($screens.Bounds.Bottom | 
          Measure-Object -Maximum).Maximum

$bounds   = [Drawing.Rectangle]::FromLTRB($left, $top, $width, $height)
$bmp      = New-Object System.Drawing.Bitmap ([int]$bounds.width), ([int]$bounds.height)
$graphics = [Drawing.Graphics]::FromImage($bmp)

$graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size)

$bmp.Save("$env:USERPROFILE\Desktop\test.jpg")

$graphics.Dispose()
$bmp.Dispose()

Update

To minimize questions. As per my comment, you'd end up doing a command like this as the last line. You are going to have to play around to find one that will consistently work for your use case(s).

[System.Windows.Forms.SendKeys]::SendWait('% n {ENTER}')

Note:

This is not unique, there are tons of others like this all over the web, right here on SO, and shown in Youtube videos.

4
  • Many thanks. Last code works perfectly for me. Commented Nov 25, 2022 at 5:59
  • I have one more query, is there any code that can be used to minimized msedge after taking screenshot? Commented Nov 25, 2022 at 6:36
  • No worries, and sure, you can just use SendKeys for GUI automation to do that. Now, understand that SendKeys can be kind of squirrelly. It can and will perform a bit differently on different hosts, i.e., things like timing, etc. There is tons of docs and samples right here on OS, all over the web and on Youtube videos covering SendKeys. AS for the code above, it's just one line. See the update. Really, if you are looking at UI Automation then look to UI Automation purpose built tools. MS UI Automation and Selinium for example.
    – postanote
    Commented Nov 25, 2022 at 20:45
  • Just curious. Why script this, when the built-in tools I pointed you to provide this feature, and hotkeys do the minimize stuff? I mean a user has to be logged on for any of this to work, and this cannot be done remotely, without using a Scheduled Task or the like, running in the context of the user.
    – postanote
    Commented Nov 25, 2022 at 20:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .