IE is dead to the world, but it’s still kickin’ in Windows 10. Here’s a great way to use the browser to automate some redundant tasks and score back some extra time.

Lately I’ve used PowerShell to automate Internet Explorer. An website’s store used a web page for managing a particular type of products and had no bulk-import available. So adding some 1400 custom products would take a significant amount of web form data re-entry, which I’m told this team has laboriously worked before. Any access to import these directly into the SQL tables by a developer or server admin is right out of the question.

I asked my colleague to give me a spreadsheet of product fields to enter on the page and exported it as a CSV.  It took a short search to discover the proper import-csv cmdlet to ingest the file, but making a loop over it to drive the IE website was really a synch thanks to Rhys Campbell’s post on instantiating an Internet Explorer COM object in PowerShell (there used to automate Twitter). My implementation was a bit difficult because most page form elements didn’t have ID attributes and using names took a few tries to reference them, but it got the job done.

The code for this is below:

# Author: Shawn Keene, adapted from Rhys Campbell
# Date: 8/26/2015
# Powershell script to automate Internet Explorer

$username = "sarah";
$password = "monkey";
$loginUrl = "";
$shopID = "A1C";
$AddProdURL = "";
$iterator = 1;

#initialize browser
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1; }    #wait for browser idle

($ie.document.getElementsByName("username") |select -first 1).value = $username;
($ie.document.getElementsByName("password") |select -first 1).value = $password;
($ie.document.getElementsByName("login") |select -first 1).click();
while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1; }    #wait for browser idle

#choose shop
($ie.document.getElementsByName("shop") |select -first 1).value = $shopID;
($ie.document.getElementsByName("submit") |select -first 1).click();
while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1; }    #wait for browser idle

start-sleep -seconds 2

$products = import-csv products.csv | foreach { 
    write-host Product $iterator -  $_.ITEM_NUMBER
    #go to product addition form
    while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1; }    #wait for browser idle

    #fill out form fields
    ($ie.document.getElementsByName("item_number") |select -first 1).value = $_.ITEM_NUMBER;
    ($ie.document.getElementsByName("item_name") |select -first 1).value = $_.ITEM_NAME;
    ($ie.document.getElementsByName("item_type") |select -first 1).value = "C";
    ($ie.document.getElementsByName("short_description") |select -first 1).value = $_.ShortProductDescription;
    ($ie.document.getElementsByName("long_description") |select -first 1).value = $_.LongDescription;
    ($ie.document.getElementsByName("legal") |select -first 1).click();
    ($ie.document.getElementsByName("legal_text") |select -first 1).value = $_.LegalText;
    ($ie.document.getElementsByName("submit") |select -first 1).click();

    $iterator = $iterator+1;

Write-Host -ForegroundColor Green "All Done!";

Why is this better than other automation like AutoIT Scripts or other UI automation? A few reasons:

  1. Communication back to the script allows it to be resilient to changing network latency and conditions. The script can check the status of the browser and not proceed until the page is loaded, or even check the right status codes are received or that you weren’t redirected to another page.
  2. It doesn’t hog your UI.  It doesn’t need to send keystrokes or mouse clicks, or emulate anything in the user space. So feel free to work on other tasks while this does your jobs. Or, set the visible parameter to false the beginning of the script and let it run in the background silent and invisible.  (Or set it to visible and tell your boss you need to take a long lunch).

For the full reference of what you can do and available methods, take a look at the documentation here: Scripting Object Interfaces (MSHTML).

Bonus Chatter: How did I get the color-formatted script into my HTML? Notepad++ makes it easy with a default included plugin. I copied my script from the PowerShell editor into here and copied out with colors. This is great for sharing code by email or other mediums where syntax highlighting helps visibility (always).