0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Replace Character in String

Use the PowerShell operator -replace like this: $newString = $oldString -replace 'oldChar', 'newChar' to replace characters in a string.
📋

Examples

Inputhello world
Outputhello_world
Input2023-06-15
Output2023/06/15
Inputnochange
Outputnochange
🧠

How to Think About It

To replace a character in a string, think of scanning the string and swapping every occurrence of the old character with the new one. PowerShell has a simple operator -replace that does this in one step.
📐

Algorithm

1
Get the original string input.
2
Identify the character to replace and the new character.
3
Use the replace operation to swap all occurrences of the old character with the new one.
4
Return or print the modified string.
💻

Code

powershell
$oldString = "hello world"
$newString = $oldString -replace ' ', '_'
Write-Output $newString
Output
hello_world
🔍

Dry Run

Let's trace replacing space with underscore in 'hello world' through the code

1

Original string

$oldString = "hello world"

2

Replace space with underscore

$newString = $oldString -replace ' ', '_' # Result: 'hello_world'

3

Output result

Write-Output $newString # Prints 'hello_world'

StepString Value
Initialhello world
After replacehello_world
💡

Why This Works

Step 1: Use of -replace operator

The -replace operator in PowerShell searches the string for the specified old character and replaces all its occurrences with the new character.

Step 2: Assigning the result

The result of the replacement is stored in a new variable so the original string remains unchanged unless reassigned.

Step 3: Outputting the new string

Using Write-Output prints the modified string to the console for verification.

🔄

Alternative Approaches

Using .Replace() method
powershell
$oldString = "hello world"
$newString = $oldString.Replace(' ', '_')
Write-Output $newString
This method is a string class method and works similarly but is case-sensitive and does not support regex.
Using -creplace for case-sensitive replacement
powershell
$oldString = "Hello World"
$newString = $oldString -creplace ' ', '_'
Write-Output $newString
-creplace is case-sensitive, useful when you want exact character matching.

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

Time Complexity

The operation scans each character in the string once, so time grows linearly with string length.

Space Complexity

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

Which Approach is Fastest?

The .Replace() method is generally faster for simple replacements without regex, while -replace is more flexible but slightly slower.

ApproachTimeSpaceBest For
-replace operatorO(n)O(n)Flexible replacements with regex
.Replace() methodO(n)O(n)Simple, case-sensitive replacements
-creplace operatorO(n)O(n)Case-sensitive regex replacements
💡
Use -replace for simple character replacements and regex support in PowerShell.
⚠️
Beginners often forget that -replace uses regex, so special characters need escaping.