PowerShell Script to Convert Text to Lowercase
.ToLower() on a string to convert it to lowercase, for example: $text = "HELLO"; $lower = $text.ToLower().Examples
How to Think About It
.ToLower(). This method changes all uppercase letters to their lowercase versions while leaving other characters unchanged.Algorithm
Code
$text = "Hello World!"
$lowercaseText = $text.ToLower()
Write-Output $lowercaseTextDry Run
Let's trace the example 'Hello World!' through the code
Assign input string
$text = "Hello World!"
Convert to lowercase
$lowercaseText = $text.ToLower() # Result: "hello world!"
Output result
Write-Output $lowercaseText # Prints: hello world!
| Step | Variable | Value |
|---|---|---|
| 1 | $text | Hello World! |
| 2 | $lowercaseText | hello world! |
| 3 | Output | hello 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
$text = "HELLO"
$text | ForEach-Object { $_.ToLower() }$text = "HELLO" $lower = -join ($text.ToCharArray() | ForEach-Object { if ($_ -match '[A-Z]') { [char]([int]$_ + 32) } else { $_ } }) Write-Output $lower
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| .ToLower() method | O(n) | O(n) | Simple and fast single string conversion |
| Pipeline with ForEach-Object | O(n) | O(n) | Processing multiple strings in a pipeline |
| Manual ASCII conversion | O(n) | O(n) | Learning or custom character handling |
.ToLower() directly on your string variable for quick and easy lowercase conversion..ToLower() to a variable or output it, expecting the original string to change automatically.