Automating Internet Explorer with Powershell

Ashamed of the amount of time you spend on Twitter? Want to know how to automate Internet Explorer with Powershell? Once again Powershell comes to the rescue! Here’s an illustration I came up with to post tweets on your twitter account from a text file.

Create a text file called tweets.txt and place it into your user profile directory (this will be something like C:\Users\<username> on Vista and Windows 7 or C:\Documents and Settings\<username> on Windows XP). Here’s what I placed in mine;

Getting my head down with a little Powershell to automate IE.
Heavy snow expected over South East England tonight. Black sludge in London to follow.
Must get myself a nice warm hat.
Last Friday before Xmas. Farringdon will be rammed tonight with the usual Fabric crowd.
Going downstairs for a swim.

Next you need to modify a few variables in the Powershell script below.

$username_or_email – Your twitter username or account email address.

$password – Your twitter password.

$sleep – The interval you want between tweets.

?View Code POWERSHELL
# Author: Rhys Campbell
# Date: 18/12/2009
# Powershell script to automate Internet Explorer
# to post tweets to your Twitter account
 
# Twitter login name or email
$username_or_email = "xxxxxxxxxxxxxxxx";
# Your twitter password
$password = "xxxxxxxxxxxx";
$url = "http://twitter.com/login";
 
# txt file containing 1 tweet per line
$tweets = Get-Content "$env:USERPROFILE\tweets.txt";
#Interval between tweets
$sleep = 60; 
 
# This is just an attempt to handle the situation where you are already logged into twitter
trap [Exception]
{
	# This will happen if you're already logged in
	if($_.Exception.Message -eq "Property 'value' cannot be found on this object; make sure it exists and is settable." -Or $_.Exception.Message -eq "You cannot call a method on a null-valued expression.")
	{
		# Try and skip this error
		continue;
	}
	else
	{
		# Fail for other Exceptions
		Write-Host -ForegroundColor Red $_.Exception.Message;
	}
}
 
# Create an ie com object
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($url);
# Wait for the page to load
while ($ie.Busy -eq $true)
{
	Start-Sleep -Milliseconds 1000;
}
 
# Login to twitter
Write-Host -ForegroundColor Green "Attempting to login to Twitter.";
# Add login details
$ie.Document.getElementById("username_or_email").value = $username_or_email;
$ie.Document.getElementById("session[password]").value = $password;
# Click the submit button
$ie.Document.getElementById("signin_submit").Click();
# Wait for the page to load
while ($ie.Busy -eq $true)
{
	Start-Sleep -Milliseconds 1000;
}
 
# Loop through each tweet in the txt file
foreach($tweet in $tweets)
{
	$ie.Document.getElementById("status").value = $tweet;
	$ie.Document.getElementById("status_update_form").Submit();
	Write-Host -ForegroundColor Green "Tweeted; $tweet.";
	Start-Sleep -Seconds $sleep;
	# Check to see if ie is still busy and sleep if so
	while ($ie.Busy -eq $true)
	{
		Start-Sleep -Milliseconds 1000;
	}
}

Execute the Powershell script and get tweeting! Here’s my output from a test run;

Attempting to login to Twitter.
Tweeted; Getting my head down with a little Powershell to automate IE..
Tweeted; Heavy snow expected over South East England tonight. Black sludge in London to follow..
Tweeted; Must get myself a nice warm hat..
Tweeted; Last Friday before Xmas. Farringdon will be rammed tonight with the usual Fabric crowd..
Tweeted; Going downstairs for a swim..

Please be aware that changes Twitter make to their site may break this script so it may not function for long without changes. If you want to know more about working with IE and Powershell check out this link and follow @ScriptingGuys on twitter.


10 Comments

  1. Good one….. will try to develop few more scripts… I m now totally in love with PS

  2. TREY says:

    Here is the same for Facebook…

    # Author: Rhys Campbell modified by +—>TREY.
    # Date: 7/24/2010
    # Powershell script to automate Internet Explorer
    # to post status updates to your Facebook account

    # Facebook login name or email
    $username_or_email = “XXX”;
    # Your Facebook password
    $password = “XXX”;
    #I used the mobile URL because it loads faster…
    $url = “http://m.facebook.com/login.php”;

    # txt file containing 1 tweet per line
    $posts = Get-Content “$env:USERPROFILE\posts.txt”;
    #Interval between posts
    $sleep = 60;

    # This is just an attempt to handle the situation where you are already logged into Facebook
    trap [Exception]
    {
    # This will happen if you’re already logged in
    if($_.Exception.Message -eq “Property ‘value’ cannot be found on this object; make sure it exists and is settable.” -Or $_.Exception.Message -eq “You cannot call a method on a null-valued expression.”)
    {
    # Try and skip this error
    continue;
    }
    else
    {
    # Fail for other Exceptions
    Write-Host -ForegroundColor Red $_.Exception.Message;
    }
    }

    # Create an ie com object
    $ie = New-Object -com internetexplorer.application;
    $ie.visible = $true;
    $ie.navigate($url);
    # Wait for the page to load
    while ($ie.Busy -eq $true)
    {
    Start-Sleep -Milliseconds 1000;
    }

    # Login to Facebook
    Write-Host -ForegroundColor Green “Attempting to login to Facebook.”;
    # Add login details
    $ie.Document.getElementById(“email”).value = $username_or_email;
    $ie.Document.getElementById(“pass”).value = $password;
    # Click the submit button
    $ie.Document.getElementById(“login”).Click();
    # Wait for the page to load
    while ($ie.Busy -eq $true)
    {
    Start-Sleep -Milliseconds 1000;
    }

    # Loop through each post in the txt file
    foreach($post in $posts)
    {
    $ie.Document.getElementById(“status”).value = $post;
    $ie.Document.getElementById(“update”).Click();
    Write-Host -ForegroundColor Green “Posted; $post.”;
    Start-Sleep -Seconds $sleep;
    # Check to see if ie is still busy and sleep if so
    while ($ie.Busy -eq $true)
    {
    Start-Sleep -Milliseconds 1000;
    }
    }

  3. Rhys says:

    That’s neat, thanks for sharing.

    Rhys

  4. Bill says:

    This no longer works with the new twitter. Any chance you could post an updated version of this code?

    It logs in and stuff but it won’t tweet from the notepad.

    Would love if you could update this. Thanks

  5. Rhys says:

    Hi Bill,

    Doesn’t look like a quick fix. I’ll try and have a look this weekend.

    Cheers,

    Rhys

  6. Armaan says:

    Please share a command to access eCommerce website such as ebay amazon etc.

  7. Rhys says:

    Hi Armaan,

    Unfortunately I don’t have one for that handy! This example was just a bit of fun really. You’re probably better off looking at something like this http://pages.ebay.co.uk/help/sell/pro-sales-and-automation.html

    Cheers,

    Rhys

  8. Iliyan says:

    Hi

    I have problem with this script, it works fine on IE 8, but there are problems with the new versions of IE. Here is the error that is passes to the console of powershell:
    Cannot find an overload for “getElementById” and the argument count: “1”.
    Cannot find an overload for “getElementById” and the argument count: “1”.
    At C:gazmo.ps1:54 char:1
    + $ie.Document.getElementById(“tbUserName”).value = $username;
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

    Can someone help. Thanks in advance.

    Kind regard,

    Iliyan Iliev

Leave a Reply