Accessing the Bing Search API v5 using PowerShell

From Svendsen Tech PowerShell Wiki
Jump to: navigation, search

Here's short and simple demonstration PowerShell code of searching Bing via the Microsoft Cognitive Services Bing Web Search API in version 5, using the Invoke-RestMethod cmdlet found in PowerShell version 3 and up. PowerShell version 3 is built into Win 8 / Server 2012 (will work in all later versions, too).

Here is the last article version that described accessing the v2 API (obsolete after December 15, 2016 as Microsoft cancelled the service).

Get the API key

Go here and get the Microsoft Cognitive Services Bing Web Search API key.

Performing the search

This screenshot demonstrates a sample Bing search in PowerShell, asking the Microsoft Cognitive Services Bing Web Search API v5, for web search results for "Bill Gates".

Bing-Search-API-v5-PowerShell-example-Invoke-RestMethod.png




Here's the same example as text, for pasting, etc.:

PS C:\temp> $BingQuery = "bill gates"

PS C:\temp> # $MyBingApiKey = 'your key here' # set earlier

PS C:\temp> $WebSearch = Invoke-RestMethod -Uri `
"https://api.cognitive.microsoft.com/bing/v5.0/search?q=$BingQuery&count=3&mkt=en-us" `
-Headers @{ "Ocp-Apim-Subscription-Key" = $MyBingApiKey } 

Looking at the results

I have a look at the type of the returned JSON, which the cmdlet Invoke-RestMethod has converted to a custom PowerShell object (see the type).

PS C:\temp> $WebSearch.GetType().FullName
System.Management.Automation.PSCustomObject

PS C:\temp> $WebSearch


_type           : SearchResponse
webPages        : @{webSearchUrl=https://www.bing.com/cr?IG=027A6C7631D3475FBF447BB2CD2B87A9
                  &CID=05CE6A693AB46EAE0DDA63883B536F31&rd=1&h=W8hngOM84FSaRr8SkjXGZZHmLQ9nw
                  PfNLzvKn-f8V2Q&v=1&r=https%3a%2f%2fwww.bing.com%2fsearch%3fq%3dbill%2bgate
                  s&p=DevEx,5395.1; totalEstimatedMatches=12400000; value=System.Object[]}
images          : @{id=https://api.cognitive.microsoft.com/api/v5/#Images; readLink=https://
                  api.cognitive.microsoft.com/api/v5/images/search?q=bill+gates&qpvt=bill+ga
                  tes&qpvt=bill+gates; webSearchUrl=https://www.bing.com/cr?..... more stuff
news            : @{id=https://api.cognitive.microsoft.com/api/v5/#News; readLink=https://ap
                  i.cognitive.microsoft.com/api/v5/news/search?q=Bill+Gates; 
                  value=System.Object[]}
relatedSearches : @{id=https://api.cognitive.microsoft.com/api/v5/#RelatedSearches; 
                  value=System.Object[]}
videos          : @{id=https://api.cognitive.microsoft.com/api/v5/#Videos; readLink=https://
                  api.cognitive.microsoft.com/api/v5/videos/search?q=bill+gates; webSearchUr
                  l=https://www.bing.com/......... more stuff
rankingResponse : @{mainline=; sidebar=}

It looks like the "webPages" property could be interesting, so I have a look at that.

PS C:\temp> $WebSearch.webPages

To avoid cluttering the article with needless information I won't paste the results of that output here, but rather mention that you get a "webSearchUrl", 'totalEstimatedMatches" and "value" back. The parts I'm after appear to be under "value", so I dig into that.

PS C:\temp> $WebSearch.webPages.value | select -first 1


id              : https://api.cognitive.microsoft.com/api/v5/#WebPages.0
name            : Bill Gates - Wikipedia
url             : https://www.bing.com/cr?IG=027A6C7631D3475FBF447BB2CD2B87A9&CID=05CE6A693A
                  B47EAE0DCA63883B536F31&rd=1&h=sJ3MQUNecp3xt9gRahWs1jk3wBVjUYXg6DLuv5cGQMU&
                  v=1&r=https%3a%2f%2fen.wikipedia.org%2fwiki%2fBill_Gates&p=DevEx,5093.1
about           : {@{name=Bill Gates}}
displayUrl      : https://en.wikipedia.org/wiki/Bill_Gates
snippet         : William Henry "Bill" Gates III (born October 28, 1955) is an American 
                  business magnate, investor, author and philanthropist. In 1975, Gates and 
                  Paul Allen ...
dateLastCrawled : 2016-12-02T15:50:00

Then we can get the parts we want out of that, for instance name/title and display URL ("real URL").

Bing-Search-API-v5-PowerShell-example-search-results.png

PS C:\temp> $WebSearch.webPages.value | select Name, DisplayUrl | ft -AutoSize

name                       displayUrl                                 
----                       ----------                                 
Bill Gates - Wikipedia     https://en.wikipedia.org/wiki/Bill_Gates   
Bill Gates - Biography.com www.biography.com/people/bill-gates-9307520
Bill Gates | Facebook      https://www.facebook.com/BillGates