Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun: Search Me

Posted on January 16, 2015

magnifying-glass-text-label-search I've been working on a few revisions and additions to my ISE Scripting Geek module. Even though what I'm about to show you will eventually be available in a future release of that module, I thought I'd share it with you today. One of the things I wanted to be able to do was search Google (or Bing) from within the PowerShell ISE.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

If you research search techniques or APIs for sites like Google and Bing, you'll quickly get swallowed up in the miasma of JSON, SOAP, XML and mind-numbing examples. Although you might enjoy this sort of thing, I was hoping to find something much, much simpler. After all, I'm not going to be creating complicated search queries. I'm going to want to search for a simple term or phrase like "powershell remoting" or "Invoke-Webrequest".

For Google, it is this simple to create a URL:

$t = "Invoke-Webrequest"
$url = "http://www.google.com/search?q=$t"

To use Bing, it seems it helps to include your culture which you can retrieve with Get-Culture.

$lang = (get-culture).parent.name
$url = "http://www.bing.com/search?q=$t+language%3A$lang"

In either case I now have an appropriate URL ready to go into my default browser. I can't guarantee this will work for everyone, but it is as simple as this command:

Start $url

I'm actually using the alias for Start-Process but in this case I don't mind. Windows will switch to my browser and open the URL in a new tab. To make all of this a bit more flexible I put together a simple function.

Function Get-SearchResult {
[cmdletbinding()]
Param(
[string]$Text,
[ValidateSet("Bing","Google","Yahoo")]
[string]$SearchEngine="Google"
)

Switch ($SearchEngine) {
"Bing" {
        $lang = (get-culture).parent.name
        $url = "http://www.bing.com/search?q=$text+language%3A$lang"
        Break
       }
"Google" {
        $url = "http://www.google.com/search?q=$text"
         }
"Yahoo" {
        $url = "http://search.yahoo.com/search?p=$text"
        }
} #switch

write-Verbose "Opening $url in $SearchEngine"

Start-process $url

} #end function

The function needs a string of text to search for and a search engine. I've set a default to Google. You'll also see that I've included parameter validation so that you can only use a value in a pre-defined set. To make the function a bit more interesting I also included Yahoo. I've used a Switch statement to determine how to construct the url. You could use this function from either the console or the PowerShell ISE. I suppose in terms of PowerShell this is a "low-tech" approach but it works and meets my needs.

To plug this into the ISE, you need to dot source the function in the ISE. Then if you want, you can add it as a menu item.

$psise.CurrentPowerShellTab.AddonsMenu.Submenus.Add("Search Internet",{Get-SearchResult -Text $psise.CurrentFile.Editor.SelectedText},"ALT+S") 

This will add a menu shortcut to the Add-Ons menu called "Search Internet". I've also included an optional keyboard shortcut ALT+S, but you can define anything that isn't already being used. When the menu item is invoked, it will run my Get-SearchResult function and use the currently selected text for the -Text parameter. The net result is a Google search for whatever I have selected in a script file. If you prefer Bing, I'd simply modify the function and set it as the default.

This isn't rocket science but hopefully makes your PowerShell experience a little more efficient. You are welcome to try this now, or wait for the next version of the ISE Scripting Geek.

Have a great weekend.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d