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
$ie.Navigate("http://www.your.website.here.com/awesome_stuff/DOC_1.docx")

#------------------------------
#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')
$obj.SendKeys('s')

#Hit "Enter" to save the file
$obj.SendKeys('{Enter}')

#Closes IE Downloads window
$obj.SendKeys('{TAB}')
$obj.SendKeys('{TAB}')
$obj.SendKeys('{TAB}')
$obj.SendKeys('{Enter}')


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

    ReplyDelete