Table of Contents
Capitalize First Letter in PowerShell
Using Substring() with ToUpper() Method
To capitalize the first letter of String in PowerShell:
- Capitalize first letter using
ToUpper()chainedSubstring()method via dot notation. - Save the rest of the string in a variable.
- Use concatenation operator (
+) to concatenate the strings, received from step 1 & 2.
|
1 2 3 4 5 6 7 |
$string = "how are you?" $firstLetter = $string.Substring(0,1).ToUpper() $restOfTheString = $string.Substring(1) $string = $firstLetter + $restOfTheString $string |
|
1 2 3 |
How are you? |
First, we declared and initialized the $string variable with a string value "how are you?". Next, we retrieved the first letter of $string using $string.Substring(0,1) and capitalized it using the ToUpper() method. We saved this upper case first letter of $string in the $firstLetter variable.
After that, we saved the string after the first index of the $string in the $restOfTheString variable. Finally, both strings ($firstLetter and $restOfTheString) were concatenated using + operator.
Using replace() Method
Use the replace() method to capitalize the first letter of the string in PowerShell.
|
1 2 3 4 5 |
$string = "how are you?" $string = $string.Replace($string[0],$string[0].ToString().ToUpper()) $string |
|
1 2 3 |
How are you? |
This code is similar to the previous method. Therefore, we can say that it is the shorter form of the previous approach where we used the replace() method to replace the lowercase h with an uppercase H.
The replace() method took two arguments, the first was the value we wanted to replace, and the second was the value we wanted to replace with. Here, it will replace the first character ($string[0]) of the $string with its upper case equivalent obtained by using the ToUpper() method, while ToString() was used to convert the character to string representation.
Capitalize First Letter for each word Using ToTitleCase() Method
Use the ToTitleCase() method to capitalize the first letter of each word in the specified string in PowerShell.
|
1 2 3 4 5 6 |
$string = "john williamson" $TextInfo = (Get-Culture).TextInfo $string = $TextInfo.ToTitleCase($string) $string |
|
1 2 3 |
John Williamson |
This method is useful when working with text containing proper nouns and other words that should not be lowercase. For instance, we have name information, first name and last name. Here, we are supposed to convert the first character of every word in the given string to uppercase.
The above code snippet used two steps to capitalize each word’s first letter in the $string:
- It creates a
TextInfoobject by calling the Get-Culture cmdlet, which returns the current culture information of the system, and accessing theTextInfoproperty. - It calls the
ToTitleCase()method on theTextInfoobject, passing in the$stringas an argument.
The ToTitleCase() method capitalizes the first letter of each word in the $string and lowercase the rest of the characters, it also works with different cultures, so it can be useful when working with text in different languages. Alternatively, we also do it in the following way if we are more concerned about cultureinfo:
|
1 2 3 4 5 |
$string = "john williamson" $string = [cultureinfo]::GetCultureInfo("en-US").TextInfo.ToTitleCase($string) $string |
|
1 2 3 |
John Williamson |
This code snippet is similar to the previous one, but it explicitly specifies the culture to use for capitalization. First, it creates a CultureInfo object for the "en-US" culture by calling the GetCultureInfo() static method of the CultureInfo class and passing in "en-US" as an argument. Then it accessed the TextInfo property of the CultureInfo object and called the ToTitleCase() method on it, passing in the $string as an argument.
This way is useful when working with text in specific cultures, and we want to make sure that the capitalization is done according to the conventions of that culture. You can replace "en-US" with other culture codes to get the TextInfo of the specific culture.
Further reading:
Capitalize entire String Using ToUpper() Method
Use the ToUpper() method to capitalize the entire string in PowerShell.
|
1 2 3 4 5 |
$string = "john williamson" $string = $string.toupper() $string |
|
1 2 3 |
JOHN WILLIAMSON |
Here, we used the toupper() method to convert all the characters of the $string to uppercase. This method converts all the characters in the string to uppercase. It is useful when we want to make the string entirely uppercase and don’t want to worry about the specific culture or the proper nouns.
Remember that this method doesn’t consider the specific culture and may not work correctly with certain characters or languages.
That’s all about how to capitalize first letter in PowerShell.