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 $newSsnThis 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 $newSsnIn 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:

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 $newZipHere, 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.

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 $newPhoneNumberIn 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.

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 $newAddressIn 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:
- Count the Number of Characters in a String in PowerShell
- Check if a String Contains Special Characters in PowerShell
- Find Lines Starting with a Specific String in PowerShell
- Replace Multiple Strings in a String Using PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.