The easiest way to do this (and probably the fastest) would be to construct a hash using the state abbreviation as the key and the state code as the value.
Getting the data from an Excel file is easy if you install the ImportExcel module (available here: 7.1.0
Here's an example:
$h = @{}
$states = Import-Excel C:\junk\art.xlsx
# build a hash table
$states |
ForEach-Object{
$h[$_.State] = $_.Code
}
# get the file names
# .
# .
# .
# .
# assume that $StateName has been loaded with the name of a state
$StateName = "CO"
$StateCode = ""
if ($h.ContainsKey($StateName)){
$StateCode = $h.$StateName
}
else {
Write-Host "Oops! There's no state named $StateName in the spreadsheet!!!" -ForegroundColor Yellow
}
Write-Host "Code for state " $StateName " is " $StateCode
The spreadsheet in the example is just a simple one: