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

2 thoughts on “PowerShell – ProperCase Function

Leave a Reply

Your email address will not be published. Required fields are marked *