PowerShell – Capitalize First Letter

Capitalize First Letter in PowerShell

Capitalize First Letter in PowerShell

Using Substring() with ToUpper() Method

To capitalize the first letter of String in PowerShell:

  • Capitalize first letter using ToUpper() chained Substring() 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.

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.

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.

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:

  1. It creates a TextInfo object by calling the Get-Culture cmdlet, which returns the current culture information of the system, and accessing the TextInfo property.
  2. It calls the ToTitleCase() method on the TextInfo object, passing in the $string as 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:

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.

Capitalize entire String Using ToUpper() Method

Use the ToUpper() method to capitalize the entire string in PowerShell.

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.

Was this post helpful?

Leave a Reply

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