#PSTip How to convert words to Title Case
Let us say that you have a list of words or a sentence that you’d like to manipulate and make sure that every word starts with a capital letter. There are many ways to do that, including string manipulation or regular expression-fu. There’s actually a way that takes into count your culture settings.
The ToTitleCase() method converts a specified string to titlecase. Here’s how the signature of the methods looks like–all it takes is a string:
PS> (Get-Culture).TextInfo.ToTitleCase OverloadDefinitions ------------------- string ToTitleCase(string str)
Let’s try to convert the ‘wAr aNd pEaCe’ string:
PS> $words = 'wAr aNd pEaCe' PS> $TextInfo = (Get-Culture).TextInfo PS> $TextInfo.ToTitleCase($words) War And Peace
Note that words that are entirely in upper-case are not converted.
PS> $words = 'WAR AND PEACE' PS> $TextInfo.ToTitleCase($words) WAR AND PEACE
To ensure we always get back words with the first character in upper-case and the rest of the characters in lower-case, we explicitly convert the string to lower-case.
PS> $TextInfo.ToTitleCase($words.ToLower()) War And PeaceShare on: