Jump to page sections
- Requirements
- Example
- Create a Proper Query String
- Providing Credentials
- Constructing the URI
- Getting Search Results
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).
Requirements
Example
Create a Proper Query String
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).
Providing Credentials
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.
Constructing the URI
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"
Getting Search Results
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 GoogleBlog articles in alphabetical order
A
- A Look at the KLP AksjeNorden Index Mutual Fund
- A primitive hex version of the seq gnu utility, written in perl
- Accessing the Bing Search API v5 using PowerShell
- Accessing the Google Custom Search API using PowerShell
- Active directory password expiration notification
- Aksje-, fonds- og ETF-utbytterapportgenerator for Nordnet-transaksjonslogg
- Ascii art characters powershell script
- Automatically delete old IIS logs with PowerShell
C
- Calculate and enumerate subnets with PSipcalc
- Calculate the trend for financial products based on close rates
- Check for open TCP ports using PowerShell
- Check if an AD user exists with Get-ADUser
- Check when servers were last patched with Windows Update via COM or WSUS
- Compiling or packaging an executable from perl code on windows
- Convert between Windows and Unix epoch with Python and Perl
- Convert file encoding using linux and iconv
- Convert from most encodings to utf8 with powershell
- ConvertTo-Json for PowerShell version 2
- Create cryptographically secure and pseudorandom data with PowerShell
- Crypto is here - and it is not going away
- Crypto logo analysis ftw
D
G
- Get rid of Psychology in the Stock Markets
- Get Folder Size with PowerShell, Blazingly Fast
- Get Linux disk space report in PowerShell
- Get-Weather cmdlet for PowerShell, using the OpenWeatherMap API
- Get-wmiobject wrapper
- Getting computer information using powershell
- Getting computer models in a domain using Powershell
- Getting computer names from AD using Powershell
- Getting usernames from active directory with powershell
- Gnu seq on steroids with hex support and descending ranges
- Gullpriser hos Gullbanken mot spotprisen til gull
H
- Have PowerShell trigger an action when CPU or memory usage reaches certain values
- Historical view of the SnP 500 Index since 1927, when corona is rampant in mid-March 2020
- How Many Bitcoins (BTC) Are Lost
- How many people own 1 full BTC
- How to check perl module version
- How to list all AD computer object properties
- Hva det innebærer at særkravet for lån til sekundærbolig bortfaller
I
L
M
P
- Parse openssl certificate date output into .NET DateTime objects
- Parse PsLoggedOn.exe Output with PowerShell
- Parse schtasks.exe Output with PowerShell
- Perl on windows
- Port scan subnets with PSnmap for PowerShell
- PowerShell Relative Strength Index (RSI) Calculator
- PowerShell .NET regex to validate IPv6 address (RFC-compliant)
- PowerShell benchmarking module built around Measure-Command
- Powershell change the wmi timeout value
- PowerShell check if file exists
- Powershell check if folder exists
- PowerShell Cmdlet for Splitting an Array
- PowerShell Executables File System Locations
- PowerShell foreach loops and ForEach-Object
- PowerShell Get-MountPointData Cmdlet
- PowerShell Java Auto-Update Script
- Powershell multi-line comments
- Powershell prompt for password convert securestring to plain text
- Powershell psexec wrapper
- PowerShell regex to accurately match IPv4 address (0-255 only)
- Powershell regular expressions
- Powershell split operator
- Powershell vs perl at text processing
- PS2CMD - embed PowerShell code in a batch file
R
- Recursively Remove Empty Folders, using PowerShell
- Remote control mom via PowerShell and TeamViewer
- Remove empty elements from an array in PowerShell
- Remove first or last n characters from a string in PowerShell
- Rename unix utility - windows port
- Renaming files using PowerShell
- Running perl one-liners and scripts from powershell
S
- Sammenlign gullpriser og sølvpriser hos norske forhandlere av edelmetall
- Self-contained batch file with perl code
- Silver - The Underrated Investment
- Simple Morningstar Fund Report Script
- Sølv - den undervurderte investeringen
- Sort a list of computers by domain first and then name, using PowerShell
- Sort strings with numbers more humanely in PowerShell
- Sorting in ascending and descending order simultaneously in PowerShell
- Spar en slant med en optimalisert kredittkortportefølje
- Spre finansiell risiko på en skattesmart måte med flere Aksjesparekontoer
- SSH from PowerShell using the SSH.NET library
- SSH-Sessions Add-on with SCP SFTP Support
- Static Mutual Fund Portfolio the Last 2 Years Up 43 Percent
- STOXR - Currency Conversion Software - Open Exchange Rates API