Convert Camel Case to space delimited Display Name with PowerShell

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()

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

$newName = CamelCaseToDisplayName “groundFloorMeetingRoom”

  • Jon

    Interesting function, what about something like:

    function CamelCaseToDisplayName([string]$inString) {
    ($inString.substring(0,1).toupper() + $inString.substring(1) -creplace ‘[A-Z]’, ‘ $&’).Trim()

    The sub-string takes care of making the first character upper case, and a tiny bit of regex does the heavy lifting. The only thing is that you may now have two problems..