Techibee.com

PowerShell: How to change text to Title Case(first letter to upper case and remaining lower case)

We all know how to change a string to lower case or upper case. It is as good as invoking .Toupper() or .Tolower() methods of the string variable.

For example…

PS C:\> “TechiBee”.toupper()
TECHIBEE
PS C:\> “TechiBee”.tolower()
techibee
PS C:\>

But when it comes to changing to Title case where first letter of the word is in capital and remaining letter in world in lower case, people are using variety methods. Some people are using substring method to identify the first character to upper and remaining to lower case and then combining them to get the title case of original word/string.

Actually, It is not that hard. PowerShell has very powerfull features and you should be doing it with very easy procedures. Let us see how to do it.

PS C:\> (Get-Culture).textinfo.totitlecase(“TechiBee.com”.tolower())
Techibee.Com
PS C:\>

In the above example, I am using the ToTitleCase method that is available with Get-Culture cmdlet. All we need to do is pass the string as argument to this function to get it converted to title case. I passed “TechiBee.com” for demonstration and you can see the output. You might have noticed that “com” in “TechiBee.com” also got changed it to title case as this is also a different word in the given string. You might want to ask why we need to change it to lower case before performing the conversion. I too don’t have a answer for it but the thing is, if the given string/word has all capital letters, then this method is not changing the string to title case. Hence I used ToLower() method. You can see the below demonstration for clarity.

Hope this helps…

Exit mobile version