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