Surfing the Web the PowerShell Way

Steve Lee

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

0 comments

Discussion is closed.

Feedback usabilla icon