Monday, November 23, 2015

Automatically saving a file in IE

Requirement: Using PowerShell, go to a specific site/URL in Internet Explorer (IE), when prompted to save the file, save it, and close out of the 'Downloads'  dialog box.

Here is the PowerShell code that I came up with.  This is intended to work for a site/URL that when you navigate to it, it prompts you to save a document/file.

#Start IE and navigate to your download file/location
$ie = New-Object -Com internetExplorer.Application

#Wait for Download Dialog box to pop up
Sleep 5
while($ie.Busy){Sleep 1}

#Hit "S" on the keyboard to hit the "Save" button on the download box
$obj = new-object -com WScript.Shell
$obj.AppActivate('Internet Explorer')

#Hit "Enter" to save the file

#Closes IE Downloads window

Please let me know your feedback!!!

1 comment:

  1. A better solution is:

    Start-Sleep -seconds 3
    # Press "Alt + s" on the download dialog
    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.SendKeys]::SendWait("%n{TAB}{ENTER}") # or use SendWait("%s")
    Start-Sleep -seconds 3