0
0
PowershellConversionBeginner · 2 min read

PowerShell Script to Convert Text to Lowercase

Use the PowerShell method .ToLower() on a string to convert it to lowercase, for example: $text = "HELLO"; $lower = $text.ToLower().
📋

Examples

InputHELLO
Outputhello
InputPowerShell Script
Outputpowershell script
Input123 ABC xyz!
Output123 abc xyz!
🧠

How to Think About It

To convert text to lowercase in PowerShell, you take the original string and apply the built-in method .ToLower(). This method changes all uppercase letters to their lowercase versions while leaving other characters unchanged.
📐

Algorithm

1
Get the input string.
2
Apply the <code>.ToLower()</code> method on the string.
3
Store or output the resulting lowercase string.
💻

Code

powershell
$text = "Hello World!"
$lowercaseText = $text.ToLower()
Write-Output $lowercaseText
Output
hello world!
🔍

Dry Run

Let's trace the example 'Hello World!' through the code

1

Assign input string

$text = "Hello World!"

2

Convert to lowercase

$lowercaseText = $text.ToLower() # Result: "hello world!"

3

Output result

Write-Output $lowercaseText # Prints: hello world!

StepVariableValue
1$textHello World!
2$lowercaseTexthello world!
3Outputhello world!
💡

Why This Works

Step 1: String Method Usage

The .ToLower() method is a built-in string function in PowerShell that converts all uppercase letters in the string to lowercase.

Step 2: Preserves Non-Letter Characters

Characters that are not letters, like numbers or punctuation, remain unchanged when using .ToLower().

Step 3: Simple and Efficient

Using .ToLower() is the simplest and most efficient way to convert text to lowercase in PowerShell.

🔄

Alternative Approaches

Using pipeline with ForEach-Object
powershell
$text = "HELLO"
$text | ForEach-Object { $_.ToLower() }
This method works well when processing multiple strings in a pipeline but is more verbose for single strings.
Using -replace operator with regex
powershell
$text = "HELLO"
$lower = -join ($text.ToCharArray() | ForEach-Object { if ($_ -match '[A-Z]') { [char]([int]$_ + 32) } else { $_ } })
Write-Output $lower
This manually converts uppercase letters to lowercase using ASCII values but is more complex and less readable.

Complexity: O(n) time, O(n) space

Time Complexity

The .ToLower() method processes each character once, so the time grows linearly with the string length.

Space Complexity

A new string is created for the lowercase result, so space usage also grows linearly with input size.

Which Approach is Fastest?

Using .ToLower() is the fastest and simplest method compared to manual character processing or pipeline methods.

ApproachTimeSpaceBest For
.ToLower() methodO(n)O(n)Simple and fast single string conversion
Pipeline with ForEach-ObjectO(n)O(n)Processing multiple strings in a pipeline
Manual ASCII conversionO(n)O(n)Learning or custom character handling
💡
Use .ToLower() directly on your string variable for quick and easy lowercase conversion.
⚠️
Beginners often forget to assign the result of .ToLower() to a variable or output it, expecting the original string to change automatically.