PowerShell Internet Explorer Browser Automation

This post has an example of using PowerShell to automate IE. Every day I browse to Packt Publishing to take advantage of their free technology book of the day (https://www.packtpub.com/packt/offers/free-learning). After a few days of manually browsing I decided to write a script to do it for me. Here it is:

$ie = New-Object -ComObject InternetExplorer.Application.1

$ie.Visible = $False 
# $False is the default for Visible property, but I'll leave it here 
# so it is easy to change to True for debugging.

$ie.Silent = $True

# This pauses the script until the page loads. If this wasn't 
# here, the next commands would error out because the DOM 
# isn't loaded yet.
while($ie.Busy) {Start-Sleep -Milliseconds 100}

$email_form = $ie.Document.getElementById("email")
$email_form.value = "jake@standfastsecurity.com"
$password_form = $ie.Document.getElementById("password")
$password_form.value = "myplaintextpassword"
$submit_button = $ie.Document.getElementById("edit-submit-1")

# Similar, but less elegant than above, if the script does not 
# pause, the value of $link will be null since the page takes time 
# to load for the href to appear.
Start-Sleep -Seconds 5

$link = $ie.Document.getElementsByTagName('a') | where-object {$_.href -match 'freelearning-claim'}

And that’s it! A good post on navigating the page and filling in forms is here: https://blogs.technet.microsoft.com/heyscriptingguy/2015/05/14/use-powershell-to-check-in-for-flight/.

The InternetExplorer.Application COM Object has a lot to explore. To see all methods and properties, use the following commands to see everything the object has to offer:

$ie = New-Object -ComObject InternetExplorer.Application.1
$ie | Get-Member

Leave a Reply

Your email address will not be published. Required fields are marked *