Home > Mobile >  using powershell Save file on IE bypassing Download Popup
using powershell Save file on IE bypassing Download Popup

Time:09-18

I am trying to download file on IE Version 21H from Website using powershell.

when I click Download button using powershell, it asks me for download Popup window with below massage.

You Want to Open or Save XYZ.log from www.XYZ.com below is the code I am using

$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible=$true
$ie.Navigate("www.xyz.com/ab/sder23445sdfrty")  #please note this is random URL I provided
$link=$ie.Document.getElementsByTagName("Button") | where-object {$_.outerhtml -like "*download*"} 
$link.click()

CodePudding user response:

You can first active the IE window and bring it to front using AppActivate, then using SendKeys to send keystrokes Ctrl S to save the file.

The sample code is like below, you can change the url and element selector to your owns:

[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")

$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible=$true
$ie.Navigate("https://www.example.com/download.html") #change it to your own url
while($ie.ReadyState -ne 4 -or $ie.Busy) {Start-Sleep -m 100}
$link=$ie.Document.getElementById("btnDowload") #change it to your own selector
$link.click()

Sleep 5
$ieProc = Get-Process | ? { $_.MainWindowHandle -eq $ie.HWND }
[Microsoft.VisualBasic.Interaction]::AppActivate($ieProc.Id)
[System.Windows.Forms.SendKeys]::Sendwait("%{s}"); 
  • Related