How to Convert Strings to Lowercase or Uppercase in PowerShell?

Almost all the PowerShell scripts I have worked on, especially those related to strings, always require converting strings to lowercase or uppercase. So, I thought of writing a complete tutorial on this. In this tutorial, I will show you how to convert strings to lowercase or uppercase in PowerShell using various methods.

To convert strings to lowercase or uppercase in PowerShell, you can use the ToLower() and ToUpper() methods, which are part of the System.String .NET class. For example, $string = “Hello World”; $lowercaseString = $string.ToLower(); $uppercaseString = $string.ToUpper().

Let us check it using various methods and examples.

Convert Strings to Lowercase in PowerShell

I will show you how to convert strings to lowercase in PowerShell using different methods.

Method 1: Using ToLower() Method

You can use the PowerShell’s built-in ToLower() method to convert all characters in a string to lowercase.

Here is an example.

Let’s take the name “WASHINGTON DC” and convert it to lowercase. Below is the complete and simple PowerShell script.

# Original string
$city = "WASHINGTON DC"

# Convert to lowercase
$lowercaseCity = $city.ToLower()

# Output the result
Write-Output $lowercaseCity

This script will output:

washington dc

I executed the above PowerShell script using the popular VS code editor, and you can see the output in the screenshot below.

Convert Strings to Lowercase in PowerShell

Method 2: Using ForEach-Object with ToLower() Method

Here is another advanced example where, instead of a string, you might need to convert an array of strings to lowercase using PowerShell.

For this kind of requirement, you can use the ForEach-Object cmdlet to apply the ToLower() method to each element in an array of strings.

Here is a PowerShell script to convert an array of names to lowercase.

Let’s take an array of state names and convert them to lowercase.

# Array of state names
$states = @("California", "TEXAS", "Florida", "NEW JERSEY")

# Convert each state name to lowercase
$lowercaseStates = $states | ForEach-Object { $_.ToLower() }

# Output the result
$lowercaseStates

This script will output:

california
texas
florida
new jersey

If you execute the above PowerShell script, you will see the exact output according to the requirement, check the screenshot below:

Convert Strings to Lowercase or Uppercase in PowerShell

Method 3: Using -replace Operator for Partial String Manipulation

Sometimes, you just need to convert a part of a string to lowercase in PowerShell. This is possible using the replace operator with the ToLower() method.

Here is a complete example.

Let’s take the name “Los Angeles, CALIFORNIA” and convert only “CALIFORNIA” to lowercase.

# Original string
$location = "Los Angeles, CALIFORNIA"

# Convert "CALIFORNIA" to lowercase
$updatedLocation = $location -replace "CALIFORNIA", "CALIFORNIA".ToLower()

# Output the result
Write-Output $updatedLocation

This script will output:

Los Angeles, california

Convert Strings to Uppercase in PowerShell

Now, let us see how to convert strings to uppercase in PowerShell using various methods.

All these methods are similar to the above.

Method 1: Using ToUpper() Method

Like the ToLower() method, you can also use the ToUpper() method to convert all characters in a string to uppercase in PowerShell.

Here is an example and the complete script.

Let’s take the name “new york” and convert it to uppercase.

# Original string
$city = "new york"

# Convert to uppercase
$uppercaseCity = $city.ToUpper()

# Output the result
Write-Output $uppercaseCity

This script will output:

NEW YORK

Look at the screenshot below. I executed the above PowerShell script using VS code and it is showing the exact required output:

Convert Strings to Uppercase in PowerShell

Method 2: Using ForEach-Object with ToUpper() Method

You can use the ForEach-Object cmdlet to apply the ToUpper() method to each element in an array of strings in PowerShell. This is best suited if you are required to convert an array of strings to uppercase in PowerShell.

Let’s take an array of state abbreviations and convert them to uppercase.

# Array of state abbreviations
$stateAbbreviations = @("ca", "tx", "fl", "nj")

# Convert each abbreviation to uppercase
$uppercaseAbbreviations = $stateAbbreviations | ForEach-Object { $_.ToUpper() }

# Output the result
$uppercaseAbbreviations

This script will output:

CA
TX
FL
NJ

Method 3: Using -replace Operator for Partial String Manipulation

If you need to convert only a part of a string to uppercase in PowerShell, you can use the -replace operator combined with the ToUpper() method.

Here is a complete example of converting a part of a string to uppercase using PowerShell.

Let’s take the name “miami, florida” and convert only “miami” to uppercase.

# Original string
$location = "miami, florida"

# Convert "miami" to uppercase
$updatedLocation = $location -replace "miami", "miami".ToUpper()

# Output the result
Write-Output $updatedLocation

This script will output:

MIAMI, florida

Conclusion

In this tutorial, we checked various methods to convert strings to lowercase or uppercase in PowerShell. We used the ToLower() and ToUpper() methods for direct conversion, ForEach-Object for arrays, and the -replace operator for partial string manipulation.

I hope you have a complete idea of converting strings to lowercase and uppercase using PowerShell.

Let us know if you still have any questions or requirements; I would love to write a program for you.

You may also 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.