PowerShell Examples: Using Bing to measure the popularity of cmdlets in a specific PowerShell module

This blog is part of a series that shows example PowerShell code for those learning the language.

This time we’re using PowerShell to find out which PowerShell cmdlet in a specific module is the most popular. To do this, we’re using a Bing search and producing a list with the name and number of web pages found for each one. This is returned as an object, so you can pipe the output to sort or filter. The name of the module is hard coded to “SmbShare”, but you can replace it with any valid module name. You could also make a parameter.

This example explores using the Web client in the .NET System.Web assembly, searching through the resulting web page using string functions, showing cmdlet progress and also a trick to create an object on the fly. It also shows more common items like arrays and functions.

 

## Popularity (via Bing) of the PowerShell cmdlets in a module#

## Function to see how many pages Bing finds for a given term#

# Adds assembly to support URL encoding and the web client

Add-Type -Assembly System.Web$WebClient = New-Object system.Net.WebClient

Function Get-BingCount([string] $Term) {

    # Add plus and quotes, encodes for URL $Term = '+"' + $Term + '"' $Term = [System.Web.HttpUtility]::UrlEncode($Term)

    # Load the page as a string $URL = "https://www.bing.com/search?q=" + $Term $Page = $WebClient.DownloadString($URL)

    # searches for the string before the number of hits on the page $String1 = '<span class="sb_count">' $Index1 = $Page.IndexOf($String1)

    # if found the right string, finds the exact end of the number If ($Index1 -ne -1) { $Index1 += $String1.Length $Index2 = $Page.IndexOf(" ", $Index1) $result = $Page.Substring($Index1, $Index2 - $index1) } else { $result = "0" }

    # Return the count return $result}

## Main code#

$CmdletList = Get-Command -Module SmbShare | Select Name$CmdletCount = $CmdletList.Count -1

0..$CmdletCount | % {

    # Tracks progress Write-Progress -Activity "Checking cmdlet popularity" -PercentComplete ($_ * 100 / $CmdletCount)

    # Check the popularity with Bing $cmdlet = $CmdletList[$_].Name $count = [int] (Get-BingCount $cmdlet) # Format as a row, output it $Row = "" | Select CmdletName, BingCount $Row.CmdletName = $cmdlet $Row.BingCount = $count $Row}

Write-Progress -Activity "Checking cmdlet popularity" -Completed

# Releases resources used by the web client$WebClient.Dispose()

 

In case you were wondering what the output would look like, here is a sample:

CmdletPopularity

Comments

  • Anonymous
    April 18, 2015
    these terminology of computer language is difficult to understand but when it comes out as a result it looks good... so i like it..http://www.hexder.com/categories/Movie-Jackets/
  • Anonymous
    May 11, 2015
    If you follow the blog, you probably saw a little PowerShell script I published a while back to measure