Jump to page sections
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
Performing the search
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
PS C:\temp> $WebSearch.GetType().FullName System.Management.Automation.PSCustomObjectPS 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").
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/BillGatesPowershell Windows Bing Search
Blog 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