Use PowerShell to quickly start a new Google Search

Hi guys,

In this two-part series, I’ll demonstrate how I created a quick little function to launch a new Google search from the command line using Powershell.  It started with me being annoyed that looking up the maximum RAM for my motherboard was a multistep process, first using Get-WmiObject Win32_BaseBoard | Select Product  and then Googling it to see the max RAM.  I wanted to start a search right from the Powershell window!

To start off, I wanted to make a function accept infinite number of characters with spaces as an argument.  This became a problem as PowerShell uses spaces to delimit parameters, so I had to get a bit clever.  I didn’t want to have to create thirty or more parameters in my function header to accept all of the possible names.  I got around this by using the $args variable, which alongside the $input variable can be used to handle either Pipeline Input or the function being called itself with parameters.  Using $input leads to another problem–For here there be dragons!–the $input automatic variable is NOT an array, but rather a construct known as an enumerator.  That means two things:

  1. Unless you call its .Reset() Method, you can only access its value a single time.  One time use!
  2. You cannot use it in the Begin phase of your functions, only in the Process.

If you want to really learn about how weird these things are, check out these blogs posts here about them.

With all of that in mind, I instead created a structure in the script to detect if there are arguments present, and if not, set $args equal to the values in $input by means of classing $input as an array (this means throwing an @ sign at the front, and robing it in a nice warm pair of parentheses), and then running the .Split() Method on $args, to give us separate objects (necessary to build a URL).

The code reads the content of $args (which will be similar whether the function receives a parameter or pipeline input) and then for each object in $args, throws it onto a $query variable with a plus sign at the end of it.  In the last step of the code, $query is substringed to remove a dangling plus sign, and then committed to $url, which is then invoked using the Start command, to open in your browser of choice.

Here is the code.

<#
.Synopsis
Searches the Googes
.DESCRIPTION
Lets you quickly start a search from within Powershell
.EXAMPLE
Search-Google Error code 5
--New google search results will open listing top entries for 'error code 5'

.EXAMPLE
search-google (gwmi win32_baseboard).Product maximum ram

If you need to get the maximum ram for your motherboard, you can even use this
type of syntax
#>
function Search-Google
{
Begin
{
$query='https://www.google.com/search?q='
}
Process
{
if ($args.Count -eq 0)
{
"Args were empty, commiting `$input to `$args"
Set-Variable -Name args -Value (@($input) | % {$_})
"Args now equals $args"
$args = $args.Split()
}
ELSE
{
"Args had value, using them instead"
}

Write-Host $args.Count, "Arguments detected"
"Parsing out Arguments: $args"
for ($i=0;$i -le $args.Count;$i++){
$args | % {"Arg $i `t $_ `t Length `t" + $_.Length, " characters"} }

$args | % {$query = $query + "$_+"}

}
End
{
$url = $query.Substring(0,$query.Length-1)
"Final Search will be $url `nInvoking..."
start "$url"
}
}

   And here is the result.

Search-google01

And a new browser tab was born…

Search-google02

 

In the end, you can use this to get the maximum RAM, just as I wanted to!

search-google (gwmi win32_baseboard | select -expand Product) maximum ram

If you want to add this to your default Powershell window (and why wouldn’t you?), simply add it to your PowerShell Profile, which is launched every time you open the program.

I said this is a two-parter, so next time we’ll go through how to setup a Custom Search Engine in Google to get the keys needed to do your own search right from the PowerShell window!  In the end, we’ll be able to see the top search results within our Powershell window, and then type a number to launch the desired output in a new browser tab.  COOL!

Advertisements

7 thoughts on “Use PowerShell to quickly start a new Google Search

  1. notrix May 30, 2014 / 11:43 pm

    Awesome stuff here man.

    • FoxDeploy May 30, 2014 / 11:47 pm

      Happy you like it. This has been in my $profile for almost a year now! I never posted the second half, which displays the results in the powershell window! As time went on, I found myself making a Web parser in powershell and started hating life and gave up. But listing the results in the console was cool.

      I’ll dig it up and post it.

  2. alexxkidd32 June 30, 2014 / 7:44 pm

    Awesome! Works like a charm

    • FoxDeploy June 30, 2014 / 8:14 pm

      Happy you like It! Tune in next week for the updated version, with results in the console!

  3. DanP. December 29, 2014 / 6:51 pm

    Was this method necessary due to version? This seems to work just fine..

    function googletest{

    $search = $args

    $url = “https://www.google.com/#q=” + $search

    start $url

    }

    • FoxDeploy December 29, 2014 / 8:14 pm

      At the time, you had to concatenate the entries with a ‘+’ sign. Good find!

  4. Ran Lottem August 4, 2016 / 3:34 am

    Great script!
    Is there a way to turn the website to be searched into a parameter? For example “Websearch g query” would use google, etc.?

    I guess “if ($args.Count -eq 0)” would need to change to -eq 1, but can’t tell much more than that.

Have a code issue? Share your code by going to Gist.github.com and pasting your code there, then post the link here!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s