Surfing the Web the PowerShell Way

Surfing the Web the PowerShell Way

Rate This
  • Comments 12

I spend a lot of my time in the command line (surprised?). That said, I also spend a lot of time surfing the web. Here is a script that I wrote that lets me surf the web from PowerShell:

UrlTemplateMap = @{
"bs" = "http://blogsearch.google.com/blogsearch?hl=en&q={0}&ie=UTF-8&scoring=d"
"dr" = "http://drudgereport.com"
"gtr" = "http://www.google.com/language_tools?hl=en" # GOOGLE TRANSLATE
"verb" = "http://msdn2.microsoft.com/en-us/library/ms714428.aspx"
"v" = "http://msdn2.microsoft.com/en-us/library/ms714428.aspx"
"ps" = "http://blogs.msdn.com/powershell/default.aspx"
}
$Script:OutIE = $Null
function Out-IE ($url, [Switch]$Reuse)
{
if ($Script:OutIE -eq $null -OR $Script:OutIE.Application -eq $null -OR !($Reuse))
{
$Script:OutIE = New-Object -Com InternetExplorer.Application
}
if ((!$url) -OR ($url -eq "?") -OR ($url -eq "-?"))
{
$urlTemplateMap.GetEnumerator() |Sort Name |Format-Table @{Expression={$_.Name};Label="Name";Width=10},Value
return
}

$navOpenInBackGroundTab = 0
foreach ($u in @($url))
{
$templateUrl = $u
$MappedUrl = $UrlTemplateMap.$u
if ($MappedUrl)
{
$templateUrl = $MappedUrl
}

# Use the Template and $args to generage the final URL
$realUrl = $templateUrl -f $args
$Script:OutIE.Navigate2($realUrl, $navOpenInBackGroundTab)
$navOpenInBackGroundTab = 0x1000
}
$Script:OutIE.visible=1
}
Set-Alias oie Out-IE

This allows me a quick and easy way to navigate to my favorite URLs (I've included a subset of the ones I use). I put this into my profile file and then whenever I want to I can just type something like:

PS> Oie v,gtr,dr

And it will bring up IE (this is coded to Version 7 of IE) with 3 tabs for the corresponding URLS. This can also use parameterized URLS. For instance, I use google's blogsearch which takes a parameter for the search term. To search blogs for PowerShell I do this:

PS> Oie bs Powershell

This brings up a new window with all the blogs that have PowerShell in them. If I want to reuse an IE window, I use the –REUSE flag.

PS> Oie bs WINRM -reuse

Enjoy!

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 6 and 5 and type the answer here:
  • Post
  • Looks like that UrlTemplateMap doesn't work because I get the term 'UrlTemplateMap' is not recognized... so the shortwords dont work.

  • Would you mind showing an example of how you might use powershell with mshtml?

    Maybe navigate to search.live.com and fill in the search box and click ok?

    Ed.

  • hmm is there a way to specify different urls for different tabs in a new browser?

  • Ed:

    The .NET Framework offers great support for HTTP scripting. See: http://www.leeholmes.com/blog/AdvancedHTTPASPNetScriptingWithPowerShell.aspx

    Lee

  • Ed:

    http://blog.sapien.com/current/2007/5/7/live-search-in-windows-powershell.html

  • Karl:

    $ie=new-object -com internetexplorer.application

    $ie.visible=$true

    $ie.navigate($url1)

    $ie.navigate($url2,0x0800)

    "0x0800" will cause a new tab to open for $url2.

  • Karl,

    I just noticed that Jeff had something to open pages in different tabs.

    What exactly were you looking for?  To open pages in *specific* tabs like tab #1, #2, etc.?

  • I made a slight improvement to your script by using favorites in a PSLinks favorites folder to define the mapping instead of a hashtable.  See: http://www.thehubbards.org/blog/2007/08/05/surfing-the-web-the-powershell-way/

  • Is there any way to click urls, buttons, assign values to textboxes etc in a web form?

    I saw somewhere to assign values to test boxes, [get-inputElement $ie 'TextBox1'] is used. what if there is no id to the control? how to locate it. Does powershell support accessing controls via DOM?

  • I'll be using this as a method of keeping my IIS/.net sites warm, so users won't get the initial compile hit.

    Thanks.

  • I just went to use it and found

    the 1st line needs the $

    UrlTemplateMap = @{

    vs

    $UrlTemplateMap = @{

    to work:)

  • Hi all,

    I am researching powershell

    I need to do the following things. I want to know whether I can do it by powershell.

    task:

    1. access a website: http://website/****.aspx

    2. the website will return a xml

    3. I need to analyze the xml and do some logic

    can I ?

Page 1 of 1 (12 items)