PowerShell: Uppercase the First Letter of Each Word

Recently, I got a requirement to convert the first letter of each word to uppercase in PowerShell. I tried different methods and tried to share. In this tutorial, I will show you how to uppercase the first letter of each word in PowerShell using different methods.

To capitalize the first letter of each word in a string using PowerShell, you can use the ToTitleCase() method from the .NET framework, which is accessible through PowerShell. First, create a TextInfo object using (Get-Culture).TextInfo and then apply $TextInfo.ToTitleCase($string.ToLower()) to the string.

Method 1: Using ToTitleCase()

The most straightforward way to capitalize the first letter of each word in PowerShell is by using the ToTitleCase() method. This method is part of the .NET framework and is accessible through PowerShell. Let me show you an example with the complete script.

Example

# Create a TextInfo object
$TextInfo = (Get-Culture).TextInfo

# Sample string
$string = "this is a sample string"

# Capitalize the first letter of each word
$result = $TextInfo.ToTitleCase($string.ToLower())

# Output the result
Write-Output $result

In this example, we first get the TextInfo object from the current culture. We then use the ToTitleCase() method to capitalize the first letter of each word in the string. Note that we call ToLower() on the string to ensure all other letters are lowercase before applying ToTitleCase().

You can see the exact output in the screenshot below after I executed the above PowerShell script using VS code.

powershell uppercase first letter of each word

Check out Extract Text from Strings Between Delimiters Using PowerShell

Method 2: Using String Manipulation

You can also use the string manipulation techniques to uppercase the first letter of each word in PowerShell. This method involves splitting the string into words, capitalizing each word, and then joining them back together.

Let me show you an example to help you understand it better.

Example

# Sample string
$string = "this is another example string"

# Split the string into words
$words = $string.Split(" ")

# Capitalize the first letter of each word
$capitalizedWords = $words | ForEach-Object {
    $_.Substring(0,1).ToUpper() + $_.Substring(1).ToLower()
}

# Join the words back into a single string with spaces
$result = $capitalizedWords -join " "

# Output the result
Write-Output $result

In this example, we split the string into an array of words using the Split() method. We then iterate over each word, capitalize the first letter using Substring() and ToUpper(), and convert the rest of the word to lowercase with ToLower(). Finally, we join the words back together into a single string.

Here is the output you can see in the screenshot below:

powershell make first letter capital

Read Count Occurrences of a Substring in a String in PowerShell

Method 3: Using Regular Expressions

You can also use regular expressions in PowerShell. PowerShell allows you to use regex for more complex text manipulations. This method can be handy for handling specific cases or patterns.

Here is the complete script.

Example

# Sample string
$string = "yet another example string"

# Use regex to capitalize the first letter of each word
$result = [regex]::Replace($string, '\b\w', { $args[0].Value.ToUpper() })

# Output the result
Write-Output $result

In this example, we use the Replace() method from the [regex] class. The pattern \b\w matches the first letter of each word (where \b is a word boundary and \w is any word character). The replacement function { $args[0].Value.ToUpper() } converts the matched character to uppercase.

You can see the output in the screenshot below:

uppercase first letter of each word using powershell

Conclusion

In this tutorial, I have explained how to capitalize the first letter of each word in a string in PowerShell using various methods.

  • ToTitleCase() is ideal for simple and quick capitalization tasks.
  • String Manipulation offers more control and customization.
  • Regular Expressions provide powerful pattern matching and text transformation capabilities.

I recommend using the ToTitleCase() PowerShell method to capitalize the first letter of each word in a string.

You may like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.