Create U.S. State Abbreviations from State Names

function shortenState([string]$longName) {
# LK 3/17/15
# Takes upper, lower, or mixed case state name
# and returns the two-letter abbreviation.
# Example call:
#   shortenstate Vermont     <-don't use parentheses
#   "Vermont" | shortenState <- sent via a pipe
# Note that this function has to appear *before* it is called in a Powershell script.
Switch ($LongName.ToUpper()) {

  ($LongName="ALABAMA")        {$shortenState="AL"}
  ($LongName="ALASKA")         {$shortenState="AK"}
  ($LongName="ARIZONA")        {$shortenState="AZ"}
  ($LongName="ARKANSAS")       {$shortenState="AR"}
  ($LongName="CALIFORNIA")     {$shortenState="CA"}
  ($LongName="COLORADO")       {$shortenState="CO"}
  ($LongName="CONNECTICUT")    {$shortenState="CT"}
  ($LongName="DELAWARE")       {$shortenState="DE"}
  ($LongName="FLORIDA")        {$shortenState="FL"}
  ($LongName="GEORGIA")        {$shortenState="GA"}
  ($LongName="HAWAII")         {$shortenState="HI"}
  ($LongName="IDAHO")          {$shortenState="ID"}
  ($LongName="ILLINOIS")       {$shortenState="IL"}
  ($LongName="INDIANA")        {$shortenState="IN"}
  ($LongName="IOWA")           {$shortenState="IA"}
  ($LongName="KANSAS")         {$shortenState="KS"}
  ($LongName="KENTUCKY")       {$shortenState="KY"}
  ($LongName="LOUISIANA")      {$shortenState="LA"}
  ($LongName="MAINE")          {$shortenState="ME"}
  ($LongName="MARYLAND")       {$shortenState="MD"}
  ($LongName="MASSACHUSETTS")  {$shortenState="MA"}
  ($LongName="MICHIGAN")       {$shortenState="MI"}
  ($LongName="MINNESOTA")      {$shortenState="MN"}
  ($LongName="MISSISSIPPI")    {$shortenState="MS"}
  ($LongName="MISSOURI")       {$shortenState="MO"}
  ($LongName="MONTANA")        {$shortenState="MT"}
  ($LongName="NEBRASKA")       {$shortenState="NE"}
  ($LongName="NEVADA")         {$shortenState="NV"}
  ($LongName="NEW HAMPSHIRE")  {$shortenState="NH"}
  ($LongName="NEW JERSEY")     {$shortenState="NJ"}
  ($LongName="NEW MEXICO")     {$shortenState="NM"}
  ($LongName="NEW YORK")       {$shortenState="NY"}
  ($LongName="NORTH CAROLINA") {$shortenState="NC"}
  ($LongName="NORTH DAKOTA")   {$shortenState="ND"}
  ($LongName="OHIO")           {$shortenState="OH"}
  ($LongName="OKLAHOMA")       {$shortenState="OK"}
  ($LongName="OREGON")         {$shortenState="OR"}
  ($LongName="PENNSYLVANIA")   {$shortenState="PA"}
  ($LongName="RHODE ISLAND")   {$shortenState="RI"}
  ($LongName="SOUTH CAROLINA") {$shortenState="SC"}
  ($LongName="SOUTH DAKOTA")   {$shortenState="SD"}
  ($LongName="TENNESSEE")      {$shortenState="TN"}
  ($LongName="TEXAS")          {$shortenState="TX"}
  ($LongName="UTAH")           {$shortenState="UT"}
  ($LongName="VERMONT")        {$shortenState="VT"}
  ($LongName="VIRGINIA")       {$shortenState="VA"}
  ($LongName="WASHINGTON")     {$shortenState="WA"}
  ($LongName="WEST VIRGINIA")  {$shortenState="WV"}
  ($LongName="WISCONSIN")      {$shortenState="WI"}
  ($LongName="WYOMING")        {$shortenState="WY"}
Default {$shortenState="XX"}
  } #switch

return $shortenState

} # function shortenState

# The following lines import a .csv file, and modify the state field
# to the two-letter abbreviation 

$Deliveries=Import-Csv "c:userslarrypowershellworldship.csv"

$Deliveries | Foreach-Object ($_) {

  if ($_.State.length -gt 2) {$_.State = shortenState($_.State)}



One thought on “Create U.S. State Abbreviations from State Names

  1. Jeffery Hicks (@JeffHicks)

    Is there a reason your function doesn’t use a standard verb-noun naming convention like Get-StateAbbreviation? If you wanted you could create aliases like shortenstate or even gsa to make it easier to use from a command prompt.

    The same is true of parameter names. Wherever possible you should use commonly used parameter names. In this case Name would probably suffice. Your goal should be to make your functions easy to find and use like any other PowerShell cmdlet.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s