How to Convert a String to Title Case in PowerShell?

While working for a client, I got a requirement to convert a string to title case using PowerShell. I tried different methods for this. In this tutorial, I will show you how to convert a string to Title case in PowerShell using various methods. Title case means capitalizing the first letter of each word in a string while converting the rest of the characters to lowercase.

To convert a string to title case in PowerShell, you can use the .NET TextInfo class, which provides a straightforward method. Create a CultureInfo object and access its TextInfo property to call the ToTitleCase method, like this:

$cultureInfo = [System.Globalization.CultureInfo]::CurrentCulture
$textInfo = $cultureInfo.TextInfo
$titleCaseString = $textInfo.ToTitleCase("your sample string")

This will convert “your sample string” to “Your Sample String” efficiently and effectively.

Convert a String to Title Case in PowerShell

Now, let me show you different methods to convert a string to title case in PowerShell with examples.

1. Using the .NET TextInfo Class

The .NET framework provides a convenient way to convert strings to title case using the TextInfo class in PowerShell.

Here is an example to help you understand it better.

# Create a CultureInfo object
$cultureInfo = [System.Globalization.CultureInfo]::CurrentCulture

# Access the TextInfo property
$textInfo = $cultureInfo.TextInfo

# Convert string to title case
$string = "this is a sample string"
$titleCaseString = $textInfo.ToTitleCase($string)

Write-Output $titleCaseString

In this example, we first create a CultureInfo object representing the current culture. Then, we access the TextInfo property of this object, which provides the ToTitleCase method. Finally, we convert the string to title case.

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

Convert a String to Title Case in PowerShell

2. Custom PowerShell Function

You can create a custom function in PowerShell to convert a string to the title case.

Here is the complete PowerShell script for better understanding.

function Convert-ToTitleCase {
    param (
        [string]$inputString
    )

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

    # Convert each word to title case
    $titleCaseWords = $words | ForEach-Object {
        if ($_ -ne "") {
            $_.Substring(0, 1).ToUpper() + $_.Substring(1).ToLower()
        }
    }

    # Join the words back into a single string
    $titleCaseString = -join ($titleCaseWords -join " ")

    return $titleCaseString
}

# Example usage
$string = "this is another sample string"
$titleCaseString = Convert-ToTitleCase -inputString $string

Write-Output $titleCaseString

This function splits the input string into individual words, converts the first letter of each word to uppercase and the rest to lowercase, and then joins the words back together.

Check out Convert Strings to Lowercase or Uppercase in PowerShell

3. Using Regular Expressions

You can also use regular expressions in PowerShell to convert strings to title case using regex to perform complex string manipulations.

Let me show you a complete example of this.

function Convert-ToTitleCaseRegex {
    param (
        [string]$inputString
    )

    # Use regex to capitalize the first letter of each word
    $titleCaseString = $inputString -replace '\b(\w)', { $_.Value.ToUpper() }

    return $titleCaseString
}

# Example usage
$string = "yet another sample string"
$titleCaseString = Convert-ToTitleCaseRegex -inputString $string

Write-Output $titleCaseString

In this function, the -replace operator is used with a regex pattern to find the first letter of each word (\b(\w)) and convert it to uppercase.

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

PowerShell Convert a String to Title Case

Check out Convert Multiline String to Single Line in PowerShell

4. Using LINQ in PowerShell

This method is a little more advanced. You can use LINQ (Language Integrated Query) for this. This method requires familiarity with .NET and LINQ.

Here is an example.

Add-Type -TypeDefinition @"
using System;
using System.Linq;

public class StringHelper {
    public static string ToTitleCase(string input) {
        return string.Join(" ", input.Split(' ').Select(word => char.ToUpper(word[0]) + word.Substring(1).ToLower()).ToArray());
    }
}
"@

# Example usage
$string = "this is a linq example"
$titleCaseString = [StringHelper]::ToTitleCase($string)

Write-Output $titleCaseString

In this example, we define a C# class with a method to convert a string to title case using LINQ. We then call this method from PowerShell.

Conclusion

In this tutorial, I have explained how to convert a string to title case in PowerShell using various methods like:

  • Using the .NET TextInfo Class
  • Custom PowerShell Function
  • Using Regular Expressions
  • Using LINQ in PowerShell

The best method I recommend is using the .Net TextInfo class and regular expressions.

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.