Currently (March 2013), Google allows you up to 100 free searches per day. This is based on v1 of the JSON/REST API.
You will have to continue your search if you're stuck with PowerShell version 2 or earlier; there are some code examples for web requests, REST and JSON out there (I know Joel Bennett has posted some stuff on poshcode.org).See this article for how to access the Microsoft Cognitive Services Bing Web Search API (v5).
Firstly, it'll come in handy to create a little function to turn provided search terms into a proper URL-encoded query string for Google. This should do:
function Get-GoogleCSEQueryString { param([string[]] $Query) Add-Type -AssemblyName System.Web # To get UrlEncode() $QueryString = ($Query | %{ [Web.HttpUtility]::UrlEncode($_)}) -join '+' # Return the query string $QueryString }
Get that function into your current scope (put in file and dot-source, or just paste it into the console).
You can do this however you want. I created a file that I dot-source to get the (global) variables $GoogleCSEAPIKey and $GoogleCSEIdentifier which I then use in the query string passed to Invoke-RestMethod later.
Get the credentials:PS C:\> . E:\temp\GoogleCSECreds.ps1 Set the two global variables $GoogleCSEAPIKey and $GoogleCSEIdentifier
GoogleCSECreds.ps1 contains two variable assignments and the message:
$global:GoogleCSEAPIKey = "API Key here..." $global:GoogleCSEIdentifier = "The custom search engine/publisher ID here" 'Set the two global variables $GoogleCSEAPIKey and $GoogleCSEIdentifier'
Of course, it doesn't matter what you name the variables or how you do this so long as you get the credentials in the query.
PS C:\> $SearchString = 'fast cars'PS C:\> $QueryString = Get-GoogleCSEQueryString $SearchString
PS C:\> $Uri = "https://www.googleapis.com/customsearch/v1?key=$GoogleCSEAPIKey&cx=$GoogleCSEIdentifier&q=$QueryString"
PS C:\> $Results = Invoke-RestMethod -Uri $UriPS C:\> $Results | Select -Expand Items | Select -First 1 title, snippet, link | fl
title : FAST CARS! - YouTube snippet : Mar 28, 2006 ... NY to LA fun run 2004 called Mischief 3000 don't deny. link : http://www.youtube.com/watch?v=GQcrusOSiUY
That's it. Play around with the $Results object (pipe it to Get-Member) and get what you want from it. There's more information on Google's site about optional parameters, etc. I considered writing a wrapper module for Google CSE, but decided instead to save myself the time and just provide an example.
Powershell Windows Google All CategoriesMinimum cookies is the standard setting. This website uses Google Analytics and Google Ads, and these products may set cookies. By continuing to use this website, you accept this.