PowerShell Script to Convert Text to Uppercase
Use the PowerShell method
ToUpper() on a string variable like $text.ToUpper() to convert text to uppercase.Examples
Inputhello world
OutputHELLO WORLD
InputPowerShell 7.3
OutputPOWERSHELL 7.3
Input123abc!@#
Output123ABC!@#
How to Think About It
To convert text to uppercase in PowerShell, you take the original string and apply the built-in
ToUpper() method. This method changes all lowercase letters to their uppercase versions while leaving other characters unchanged.Algorithm
1
Get the input string from the user or a variable2
Apply the <code>ToUpper()</code> method to the string3
Store or output the resulting uppercase stringCode
powershell
$text = "hello world"
$uppercaseText = $text.ToUpper()
Write-Output $uppercaseTextOutput
HELLO WORLD
Dry Run
Let's trace the input 'hello world' through the code
1
Assign input string
$text = "hello world"
2
Convert to uppercase
$uppercaseText = $text.ToUpper() # Result: "HELLO WORLD"
3
Output result
Write-Output $uppercaseText # Prints: HELLO WORLD
| Step | Variable | Value |
|---|---|---|
| 1 | $text | hello world |
| 2 | $uppercaseText | HELLO WORLD |
| 3 | Output | HELLO WORLD |
Why This Works
Step 1: String stored in variable
The original text is stored in a variable $text so it can be manipulated.
Step 2: Use <code>ToUpper()</code> method
Calling ToUpper() on the string converts all lowercase letters to uppercase.
Step 3: Output the uppercase string
The converted uppercase string is printed using Write-Output.
Alternative Approaches
Using pipeline with ForEach-Object
powershell
"hello world" | ForEach-Object { $_.ToUpper() }This approach works well in pipelines but is less direct for single strings.
Using -replace operator with regex
powershell
"hello world" -replace '[a-z]', { param($c) $c.Value.ToUpper() }
This method is more complex and less efficient but shows regex usage for uppercase conversion.
Complexity: O(n) time, O(n) space
Time Complexity
The ToUpper() method processes each character once, so time grows linearly with string length.
Space Complexity
A new string is created for the uppercase result, so space usage is proportional to input size.
Which Approach is Fastest?
Using ToUpper() directly is the fastest and simplest method compared to pipeline or regex alternatives.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Direct ToUpper() | O(n) | O(n) | Simple and fast uppercase conversion |
| Pipeline with ForEach-Object | O(n) | O(n) | Processing multiple strings in a pipeline |
| Regex replace with scriptblock | O(n) | O(n) | Complex pattern matching with uppercase conversion |
Use
ToUpper() directly on strings for simple and fast uppercase conversion.Forgetting to assign the result of
ToUpper() to a variable or output it, since strings are immutable.