Welcome PowerShell User! This recipe is just one of the hundreds of useful resources contained in the PowerShell Cookbook.

If you own the book already, login here to get free, online, searchable access to the entire book's content.

If not, the Windows PowerShell Cookbook is available at Amazon, or any of your other favourite book retailers. If you want to see what the PowerShell Cookbook has to offer, enjoy this free 90 page e-book sample: "The Windows PowerShell Interactive Shell".

5.11 Convert a String to Uppercase or Lowercase

Problem

You want to convert a string to uppercase or lowercase.

Solution

Use the ToUpper() or ToLower() methods of the string to convert it to uppercase or lowercase, respectively.

To convert a string to uppercase, use the ToUpper() method:

PS > "Hello World".ToUpper()
HELLO WORLD

To convert a string to lowercase, use the ToLower() method:

PS > "Hello World".ToLower()
hello world

Discussion

Since PowerShell strings are fully featured .NET objects, they support many string-oriented operations directly. The ToUpper() and ToLower() methods are two examples of the many features that the String class supports. To learn what other functionality the String class supports, see Recipe 3.12.

Neither PowerShell nor the methods of the .NET String class directly support capitalizing only the first letter of a word. If you want to capitalize only the first character of a word or sentence, try the following commands:

PS > $text = "hello"
PS > $newText = $text.Substring(0,1).ToUpper() + $text.Substring(1)
PS > $newText

Hello

You can also use an advanced regular expression replacement, as described in Recipe 31.6:

"hello world" -replace '\b(\w)',{ $_.Value.ToUpper() }

One thing to keep in mind as you convert a string to uppercase or lowercase is your motivation for doing it. One of the most common reasons is for comparing strings, as shown in Example 5-6.

Example 5-6. Using the ToUpper() method to normalize strings
## $text comes from the user, and contains the value "quit"
if($text.ToUpper() -eq "QUIT") { ... }

Unfortunately, explicitly changing the capitalization of strings fails in subtle ways when your script runs in different cultures. Many cultures follow different capitalization and comparison rules than you may be used to. For example, the Turkish language includes two types of the letter I: one with a dot and one without. The uppercase version of the lowercase letter i corresponds to the version of the capital I with a dot, not the capital I used in QUIT. Those capitalization rules cause the string comparison code in Example 5-6 to fail in the Turkish culture.

Recipe 13.8 shows us this quite clearly:

PS > Use-Culture tr-TR { "quit".ToUpper() -eq "QUIT" }
False
PS > Use-Culture tr-TR { "quIt".ToUpper() -eq "QUIT" }
True
PS > Use-Culture tr-TR { "quit".ToUpper() }
QUİT

For comparing some input against a hardcoded string in a case-insensitive manner, the better solution is to use PowerShell’s -eq operator without changing any of the casing yourself. The -eq operator is case-insensitive and culture-neutral by default:

PS > $text1 = "Hello"
PS > $text2 = "HELLO"
PS > $text1 -eq $text2
True

PS > Use-Culture tr-TR { "quit" -eq "QUIT" }
True

For more information about writing culture-aware scripts, see Recipe 13.6.

See Also

Recipe 3.12, “Learn About Types and Objects”

Recipe 13.6, “Write Culture-Aware Scripts”

Recipe 31.6, “Use a Script Block as a .NET Delegate or Event Handler”