Share via


The Most Popular Hyper-V PowerShell Cmdlets - by Bing

Last week Jose Barreto made an excellent post about using PowerShell and Bing to check the popularity of various SMB PowerShell cmdlets.  Well, I could not resist doing the same with Hyper-V!  I made a slight tweak to Jose's code - by storing my data in a hashtable.  This made it easier to sort and output for later use.  Here is the code I used:

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
}

$CmdletList = Get-Command -Module Hyper-V | Select Name
$CmdletCount = $CmdletList.Count -1
$hashTable = $null
$hashTable = @{}

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)

    # Put data in a hashtable
    $hashTable.add($cmdlet, $count)
}

$hashTable.GetEnumerator() | sort value -Descending | select @{N="Cmdlet";E={$_.Name}}, @{N="Popularity";E={$_.Value}}

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

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

And the results?  Well - I am happy to let you know the top twenty Hyper-V PowerShell cmdlets are as follows:

Cmdlet Popularity
------ ----------
New-VM 411000
Get-VM 91400
Start-VM 81300
Set-VM 66300
Convert-VHD 60700
Mount-VHD 55900
Export-VM 49900
New-VHD 44600
Move-VM 44600
Stop-VM 42700
Remove-VM 42000
Import-VM 41100
Get-VMHost 29500
Save-VM 26400
Resize-VHD 24600
Restart-VM 24200
Test-VHD 22100
Get-VHD 17900
Suspend-VM 16900
Resume-VM 14300
Compare-VM 14100

Cheers,
Ben