Finding Popular Historic First Names

by Jan 16, 2013

To find popular first names for given decades, check out the function Get-PopularName. It accepts a decade between 1880 and 2000 and then uses the new and awesome Invoke-WebRequest in PowerShell 3.0 to visit a statistical website and retrieve popular first names using a regular expression.

Note: if your Internet connection requires a proxy server and/or authentication, please add the appropriate parameters to Invoke-WebRequest.

function Get-PopularName
{
  param
  (
     [ValidateSet('1880','1890','1900','1910','1920','1930','1940','1950','1960','1970','1980','1990','2000')]
    $Decade = '1950'
  )

  $regex = [regex]'(?si)<td>(\d{1,3})</td>\s*?<td align="center">(.*?)</td>\s*?<td>((?:\d{0,3}\,)*\d{1,3})</td>\s*?<td align="center">(.*?)</td>\s*?<td>((?:\d{0,3}\,)*\d{1,3})</td></tr>'

  $web = Invoke-WebRequest -UseBasicParsing -Uri "http://www.ssa.gov/OACT/babynames/decades/names$($decade)s.html"

  $html = $web.Content
  $Matches = $regex.Matches($html)
  $matches | ForEach-Object {
    $rv = New-Object PSObject | Select-Object -Property Name, Rank, Number, Gender
    $rv.Rank = [int]$_.Groups[1].Value
    $rv.Gender = 'm'
    $rv.Name = $_.Groups[2].Value
    $rv.Number = [int]$_.Groups[3].Value
    $rv

    $rv = New-Object PSObject | Select-Object -Property Name, Rank, Number, Gender
    $rv.Rank = [int]$_.Groups[1].Value
    $rv.Gender = 'f'
    $rv.Name = $_.Groups[4].Value
    $rv.Number = [int]$_.Groups[5].Value
    $rv
  } | Sort-Object Name, Rank
}

Just for the fun of it, call Get-PopularName and send it to Out-GridView:

PS> Get-PopularName -Decade 1960 | Out-GridView
PS> Get-PopularName -Decade 1960 | Where-Object Gender -eq m | Sort-Object -Property Rank | Select-Object -First 5

Name                                                   Rank                        Number Gender                       
----                                                   ----                        ------ ------                       
Michael                                                   1                        833346 m                            
David                                                     2                        734113 m                            
John                                                      3                        713581 m                            
James                                                     4                        684950 m                            
Robert                                                    5                        650935 m         

Twitter This Tip! ReTweet this Tip!