Capitalize the First Letter in PowerShell (Plus Uppercase, Lowercase and Title Case)

To capitalize the first letter in PowerShell, uppercase the first character and add the rest of the string back: $text.Substring(0, 1).ToUpper() + $text.Substring(1). For the whole string, call .ToUpper() or .ToLower(), and for every word, use ToTitleCase:

$city = 'san antonio, texas'

"First letter: $($city.Substring(0, 1).ToUpper() + $city.Substring(1))"
"Each word:    $((Get-Culture).TextInfo.ToTitleCase($city))"
"Upper:        $($city.ToUpper())"
"Lower:        $($city.ToLower())"

Output:

First letter: San antonio, texas
Each word:    San Antonio, Texas
Upper:        SAN ANTONIO, TEXAS
Lower:        san antonio, texas
PowerShell capitalize the first letter, title case, uppercase and lowercase
First letter, each word, uppercase and lowercase from one string (PowerShell 7)

None of these change $city itself. Each one returns a new string, so save the result in a variable if you need it later.

In this tutorial, I’ll show you how to capitalize the first letter in PowerShell, title-case every word, deal with ALL CAPS text and culture rules, and convert whole strings to uppercase, lowercase or camelCase. I tested the code in PowerShell 7.6 and Windows PowerShell 5.1.

How do I capitalize the first letter of a string in PowerShell?

Take the first character with the Substring() method, uppercase it, and add the rest of the string. Then decide whether the rest stays as typed or goes to lowercase:

$note = 'payment received for order 1042'
$shout = 'pAYMENT RECEIVED'

$note.Substring(0, 1).ToUpper() + $note.Substring(1)
[char]::ToUpper($note[0]) + $note.Substring(1)
$shout.Substring(0, 1).ToUpper() + $shout.Substring(1).ToLower()

Output:

Payment received for order 1042
Payment received for order 1042
Payment received

The first two lines keep the rest of the text as it was. The third is sentence case: it also lowercases the rest, which fixes text typed with Caps Lock on.

$note[0] returns a [char], not a string, so you call the static [char]::ToUpper() method on it. Both versions give the same result, so pick the one you find easier to read.

Handle empty strings and leading spaces

Substring(0, 1) fails on an empty string, which is common with blank CSV cells or optional parameters. Here’s what each version prints when you catch the error:

$text = ''
try { $text.Substring(0, 1).ToUpper() + $text.Substring(1) }
catch { "Failed. $($_.Exception.InnerException.Message)" }

Output in PowerShell 7:

Failed. Index and length must refer to a location within the string. (Parameter 'length')

Output in Windows PowerShell 5.1:

Failed. Index and length must refer to a location within the string.
Parameter name: length
PowerShell Substring error on an empty string in Windows PowerShell 5.1
Substring(0, 1) o