Convert Camel Case to space delimited Display Name with PowerShell
Written on May 13, 2013

I’m writing a very rudimentary script that has a need to convert something like “groundFloorMeetingRoom” to “Ground Floor Meeting Room”. Rather than prompt the user to enter this, I decided it’d be easier to just get the script to do it. This turned out to be a fun little exercise, resulting in the *CamelCaseToDisplayName *function below. Hopefully it can help someone!

function CamelCaseToDisplayName ([string]$inString) {
  $newString = ""
  $stringChars = $inString.GetEnumerator()
  $charIndex = 0
  foreach ($char in $stringChars) {
    # If upper and not first character, add a space
    if ([char]::IsUpper($char) -eq "True" -and $charIndex -gt 0) {
      $newString = $newString + " " + $char.ToString()
    } elseif ($charIndex -eq 0) {
      # If the first character, make it a capital always
      $newString = $newString + $char.ToString().ToUpper()
    } else {
      $newString = $newString + $char.ToString()
    }
    $charIndex++
  }
  $newString
}

It’s pretty simple to run, just throw something like this in your script:

$newName = CamelCaseToDisplayName "groundFloorMeetingRoom"

Comments/questions

There's no commenting functionality here. If you'd like to comment, please either mention me (@chris@aus.social) on Mastodon or email me. I don't have any logging or analytics running on this website, so if you found something useful or interesting it would mean a lot to hear from you.