Getting Picture URLs from Google Picture Search

by Apr 24, 2014

Invoke-WebRequest is your friend whenever you want to download information from the Internet. You could, for example, send a search request to Google and have PowerShell examine the results.

Google knows about this, too, so when you send a search query from PowerShell, Google responds with encrypted links. To get the real links, you would have to tell Google that you are not PowerShell but just a regular browser. This can be done by setting a browser agent string.

This script takes any keyword and returns the original picture sources for any picture that matches your keyword and has at least a 2 megapixel resolution:

$SearchItem = 'PowerShell'

$url = "https://www.google.com/search?q=$SearchItem&espv=210&es_sm=93&source=lnms&tbm=isch&sa=X&tbm=isch&tbs=isz:lt%2Cislt:2mp"
$browserAgent = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36'
$page = Invoke-WebRequest -Uri $url -UserAgent $browserAgent
$page.Links | 
  Where-Object { $_.href -like '*imgres*' } | 
  ForEach-Object { ($_.href -split 'imgurl=')[-1].Split('&')[0]}  

Twitter This Tip! ReTweet this Tip!