Monday, August 11, 2014

Start a Google search from within a PowerShell Console

Here is what I like to call a little fun function, this function allows you to easily search www.google.com for anything your heart desires. By default it will search for "The Overnight Admin" unless you specify with the -Search parameter like I did as an example on line 34.

The reason that just typing Start-Google at will search for "The Overnight Admin" is because within the param block I've set the $Search parameter to equal the "The Overnight Admin", but if you type Start-Google -Search "powershell" that will overwrite the $Search parameter and open the Google search page for powershell.


001
002
003
004
005
param ([Parameter(Mandatory=$false,
                  ValueFromPipelineByPropertyName=$true,
                  Position=0)]
                  $Search = "The Overnight Admin"
)


Enjoy!

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
Function Start-Google
{
<#
.Synopsis
Searches the Googes
.DESCRIPTION
Lets you quickly start a search Google from within Powershell
.EXAMPLE
Start-Google -search PowerShell
#>

                      [CmdletBinding()]
              Param ( [Parameter(Mandatory=$false,
                      ValueFromPipelineByPropertyName=$true,
                      Position=0)]
                      $Search = "The Overnight Admin",
                      [Parameter(Mandatory=$false,
                      ValueFromPipelineByPropertyName=$true,
                      Position=0)]
                      $google = "https://www.google.com/search?q="
              )
         Begin {
                    $ie = new-object -com internetexplorer.application
         }
    Process {
                $Search | ForEach-Object { $google = $google + "$_+"}
    }
 End
     {
         $url = $google.Substring(0,$google.Length-1)
         $ie.navigate( $url )
         $ie.visible = $true
 }
}
Start-Google -Search "Enter Hilarious Search Here"

No comments:

Post a Comment