Welcome to MSDN Blogs Sign in | Join | Help

Surfing the Web the PowerShell Way

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

Published Tuesday, June 26, 2007 6:04 AM by PowerShellTeam

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Surfing the Web the PowerShell Way

Looks like that UrlTemplateMap doesn't work because I get the term 'UrlTemplateMap' is not recognized... so the shortwords dont work.

Tuesday, June 26, 2007 10:21 AM by Ludvik

# re: Surfing the Web the PowerShell Way

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.

Tuesday, June 26, 2007 10:34 AM by Ed Fancher

# re: Surfing the Web the PowerShell Way

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

Tuesday, June 26, 2007 5:57 PM by karl prosser

# re: Surfing the Web the PowerShell Way

Ed:

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

Lee

Tuesday, June 26, 2007 7:13 PM by PowerShellTeam

# re: Surfing the Web the PowerShell Way

Wednesday, June 27, 2007 12:43 PM by Marco Shaw

# re: Surfing the Web the PowerShell Way

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.

Thursday, June 28, 2007 9:14 AM by Marco Shaw

# re: Surfing the Web the PowerShell Way

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.?

Thursday, June 28, 2007 9:42 AM by Marco Shaw

# re: Surfing the Web the PowerShell Way

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/

Sunday, August 05, 2007 1:41 AM by Chris

# re: 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?

Thursday, August 30, 2007 5:32 PM by maheshvd

# re: Surfing the Web the PowerShell Way

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.

Wednesday, January 30, 2008 6:09 PM by BobC

# re: Surfing the Web the PowerShell Way

I just went to use it and found

the 1st line needs the $

UrlTemplateMap = @{

vs

$UrlTemplateMap = @{

to work:)

Friday, April 11, 2008 3:19 PM by Jay

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker