How to Replace a Character in a String at a Specific Position in PowerShell?

Recently, I was working with some string manipulations, where I got a requirement to replace a character in a string at a specific position. In this tutorial, I will explain how to replace a character in a string at a specific position in PowerShell using different methods.

To replace a character in a string at a specific position in PowerShell using the Substring method, you can break the string into parts, replace the character, and concatenate the parts back together. For example, to change the first digit of the last segment in the SSN 123-45-6789 to 0, you can use:

$ssn = "123-45-6789"
$position = 7
$newChar = "0"
$newSsn = $ssn.Substring(0, $position) + $newChar + $ssn.Substring($position + 1)
Write-Output $newSsn

This method extracts parts of the string before and after the target position and inserts the new character in between.

Replace a Character in a String at a Specific Position in PowerShell

PowerShell provides various methods to replace a character in a string at a specific position.

I will show you each method with examples.

Method 1: Using Substring Method

The Substring method can be used to break the string into parts, replace the character, and then concatenate the parts back together.

Here is an example.

Let’s say you have a Social Security Number 123-45-6789 and you want to replace the first digit of the last segment with 0.

$ssn = "123-45-6789"
$position = 7
$newChar = "0"

$newSsn = $ssn.Substring(0, $position) + $newChar + $ssn.Substring($position + 1)
Write-Output $newSsn

In this example, $position is 7 because string indexing starts at 0. The Substring method is used to extract the parts of the string before and after the position, and then the new character is inserted in between.

I executed the above PowerShell script using VS code, and you can see the exact output in the screenshot below:

Replace a Character in a String at a Specific Position in PowerShell

Read Split String by Tab Character in PowerShell

Method 2: Using Array Indexing

PowerShell allows you to treat strings as arrays of characters, which can be modified directly. Here is an example of replacing a character in a string at a specific position using array indexing.

Consider a ZIP code 90210 where you want to change the second digit to 8.

$zip = "90210"
$charArray = $zip.ToCharArray()
$charArray[1] = '8'
$newZip = -join $charArray
Write-Output $newZip

Here, the ToCharArray method converts the string into an array of characters, allowing direct access to each character by its index. After modifying the desired character, the -join operator is used to convert the array back into a string.

You can see the output in the screenshot below after I executed the above PowerShell script.

PowerShell Replace a Character in a String at a Specific Position

Method 3: Using the Replace Operator with Regular Expressions

For more complex scenarios, you can use the -replace operator combined with regular expressions to replace a character in a string in a specified position.

Let me show you an example.

Suppose you have a phone number in the format (123) 456-7890 and you want to change the area code to 999.

$phoneNumber = "(123) 456-7890"
$newPhoneNumber = $phoneNumber -replace '^\(\d{3}\)', '(999)'
Write-Output $newPhoneNumber

In this example, the regular expression ^\(\d{3}\) matches the area code, and the -replace operator substitutes it with (999).

Here is the output you can see in the screenshot below, after I executed the above PowerShell script using VS code.

How to Replace a Character in a String at a Specific Position in PowerShell

Read Find Lines Starting with a Specific String in PowerShell

Method 4: Using StringBuilder for Large Strings

For large strings or frequent modifications, using StringBuilder can be more efficient. So, here is an example of how to replace a character in a string at a specific position in PowerShell using StringBuilder.

Here is an example.

Imagine an address string in which you need to replace a character at a specific position. You can write a script like the one below.

Add-Type -TypeDefinition @"
using System.Text;
public class StringBuilderHelper {
    public static string ReplaceAtPosition(string input, int position, char newChar) {
        var sb = new StringBuilder(input);
        sb[position] = newChar;
        return sb.ToString();
    }
}
"@

$address = "1600 Pennsylvania Avenue NW, Washington, DC 20500"
$newAddress = [StringBuilderHelper]::ReplaceAtPosition($address, 5, 'X')
Write-Output $newAddress

In this case, we define a C# helper class to use StringBuilder for replacing a character at a given position. Use this method if you are working with large strings.

Conclusion

In this tutorial, I have explained how to replace a character at a specific position in a string using PowerShell. There are various methods to do like you can use Substring, array indexing, the -replace operator with regular expressions or StringBuilder, etc.

I hope these examples will help 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.