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".


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 `
"$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

PS C:\temp> $WebSearch

_type           : SearchResponse
webPages        : @{webSearchUrl=
                  s&p=DevEx,5395.1; totalEstimatedMatches=12400000; value=System.Object[]}
images          : @{id=; readLink=https://
                  tes&qpvt=bill+gates; webSearchUrl= more stuff
news            : @{id=; readLink=https://ap
relatedSearches : @{id=; 
videos          : @{id=; readLink=https://
        ; webSearchUr
                  l= 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              :
name            : Bill Gates - Wikipedia
url             :
about           : {@{name=Bill Gates}}
displayUrl      :
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").


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

name                       displayUrl                                 
----                       ----------                                 
Bill Gates - Wikipedia   
Bill Gates -
Bill Gates | Facebook