Case-Correct Name Lists

by Jul 29, 2014

All PowerShell versions

Let’s assume it’s your job to update a list of names. Here is an approach that will make sure that only first letter in a name is capitalized. This approach works with double-names as well:

$names = 'some-wILD-casING','frank-PETER','fred'

Foreach ($name in $names)
{
  $corrected = foreach ($part in $name.Split('-'))
  {
    $firstChar = $part.Substring(0,1).ToUpper()
    $remaining = $part.Substring(1).ToLower()
    
    "$firstChar$remaining"
  }  
  $corrected -join '-'
}
 
Some-Wild-Casing
Frank-Peter
Fred

 

Twitter This Tip! ReTweet this Tip!