PowerShell – ProperCase Function

Change any case to Proper Case (‘ITNOTES DoT net’ to ‘Itnotes Dot Net’):
This is very useful for us during scripted user creations when the usernames and such from the source were in random cases. This script could also be useful with auto user provisioning with ILM/FIM 2010 from another datasource, we were using a VBScript that is much longer.

This script basically converts everything to lowercase then capitalizes the first letter of each word.

function ToProperCase ([String]$in)
{
 $in = $in.Tolower()
 $textInfo = [System.Threading.Thread]::CurrentThread.CurrentCulture.TextInfo  
 return $textInfo.ToTitleCase($in)
}

Example of use:
$Title = “ITNOTE DOT NET”
ToProperCase($Title)

Returns:
Itnotes Dot Net

Complete Script

function ToProperCase ([String]$in){
 $in = $in.Tolower()
 $textInfo = [System.Threading.Thread]::CurrentThread.CurrentCulture.TextInfo
 return $textInfo.ToTitleCase($in)
}
$Title = “ITNOTE DOT NET”
ToProperCase($Title)

As noted by Lee, here is a oneliner contributed by Jonathan Noble at http://www.jonoble.com/. Thanks Lee.

(Get-Culture).TextInfo.ToTitleCase(“THIS IS MY STRING”.ToLower())

I am always in favor of shorter scripts nevertheless I also favor functions, so here is a much shorter function:

function ToProperCase ([String]$in){
return (Get-Culture).TextInfo.ToTitleCase($in.ToLower())
}

Tags: , ,

Monday, March 1st, 2010 Identity Management, Powershell

2 Comments to PowerShell – ProperCase Function

  • lee dailey says:

    howdy y’all,

    you might wanna look here …
    http://www.jonoble.com/blog/2010/11/19/converting-a-string-to-proper-case-in-powershell.html

    … for a shorter and easier to read/grok version. i found yours 1st and then later found theirs. went back and swapped to the shorter version asap. [*grin*]

    thanks for the post, tho. it DID help when i needed it.

    take care,
    lee

  • ITNotes says:

    Thanks Lee!

  • Leave a Reply