When working with text files in Windows, you might often need to replace certain strings or characters. PowerShell, a powerful scripting language and command-line shell, provides several ways to perform text replacements. This blog post will guide you through different methods to replace text in files using PowerShell, complete with examples and scripts.
To replace text in a file using PowerShell, you can utilize the -replace operator along with Get-Content and Set-Content cmdlets. For example, read the file with Get-Content, use the -replace operator to substitute ‘oldText’ with ‘newText’, and then write the changes back using Set-Content. This method is efficient for both simple replacements and more complex pattern matching with regular expressions.
How to Replace Text in a File Using PowerShell
Now, let us check out different methods to replace text in a file using PowerShell.
1. Using the -replace Operator
The -replace operator is one of the most straightforward methods to replace text in PowerShell. It uses regex (regular expressions) to identify and replace text patterns.
Example:
# Replace 'oldText' with 'newText' in a string
$string = 'This is the oldText that will be replaced.'
$newString = $string -replace 'oldText', 'newText'To replace text in a file in PowerShell, you can combine Get-Content and Set-Content cmdlets with the -replace operator.
Complete Script:
# Define the file path
$filePath = 'C:\Bijay\file.txt'
# Read the content of the file
$content = Get-Content $filePath
# Replace 'oldText' with 'newText'
$content = $content -replace 'oldText', 'newText'
# Write the updated content back to the file
$content | Set-Content $filePathThis script reads the content of the specified file, replaces all occurrences of ‘oldText’ with ‘newText’, and writes the updated content back to the original file.
You can see the output in the screenshot below:

2. Using the .Replace() Method
For simple string replacements that don’t require regular expressions, you can use the .Replace() method in PowerShell. This method is case-sensitive and doesn’t support regex.
Example:
# Replace 'oldText' with 'newText' in a string
$string = 'Replace the following oldText with newText.'
$newString = $string.Replace('oldText', 'newText')Complete Script:
# Define the file path
$filePath = 'C:\Bijay\file.txt'
# Read the content of the file
$content = Get-Content $filePath
# Replace 'oldText' with 'newText' using the .Replace() method
$content = $content -replace 'oldText', 'newText'
# Write the updated content back to the file
$content | Set-Content $filePathThis script functions similarly to the previous one but uses the .Replace() method for a case-sensitive, non-regex text replacement.
3. Using PowerShell Functions for Advanced Replacements
If you need to perform more complex text replacements, you can create a custom PowerShell function.
Example:
function Replace-TextInFile {
param(
[string]$filePath,
[string]$oldText,
[string]$newText
)
# Read the file content
$content = Get-Content $filePath
# Replace the text
$newContent = $content -replace $oldText, $newText
# Write the new content back to the file
$newContent | Set-Content $filePath
}
# Call the function to replace text
Replace-TextInFile -filePath 'C:\Bijay\file.txt' -oldText 'oldText' -newText 'newText'This function, Replace-TextInFile, encapsulates the logic for reading a file, replacing text, and writing the content back to the file. It can be reused multiple times with different parameters for various files and text patterns.
4. Handling Special Characters and Escape Sequences
When dealing with special characters or escape sequences in regex, you might need to escape them properly.
Example:
# Replace 'old.Text' with 'newText' (note the dot is a special character in regex)
$string = 'This is the old.Text that will be replaced.'
$newString = $string -replace [regex]::Escape('old.Text'), 'newText'Conclusion
In this PowerShell tutorial, I have explained how to replace text in a file in PowerShell using various methods.
You may also like:
- How to Save Output to File in PowerShell?
- How to Download File from URL in PowerShell?
- Copy Files from One Folder to Another in PowerShell
- How to Get the Path of a File in 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.