How to Replace a String in Text File with PowerShell?

Recently, I got a requirement of replacing a string in a text file using PowerShell. PowerShell offers a straightforward way to replace strings in text files using its various cmdlets and operators. Here’s a step-by-step tutorial on how to replace a string in a text file using PowerShell.

To replace a string in a text file using PowerShell, use the Get-Content cmdlet to read the file, then apply the -replace operator to change the old string to the new string, and finally use Set-Content to write the updated content back to the file. Here’s a concise example: $content = Get-Content 'file.txt'; $content -replace 'oldString', 'newString' | Set-Content 'file.txt'. This replaces all occurrences of ‘oldString’ with ‘newString’ in ‘file.txt’.

Replace a String in Text File with PowerShell

Follow the below steps to replace a string in text file using PowerShell.

Step 1: Read the File Content

First, you need to read the content of the text file. You can use the Get-Content cmdlet to read the file.

$content = Get-Content -Path "C:\FilePath\file.txt"

Replace "C:\FilePath\file.txt" with the actual path to your text file.

Step 2: Replace the String

To replace a string, you can use the -replace operator. The syntax is $content -replace 'oldString', 'newString'.

$updatedContent = $content -replace 'oldString', 'newString'

Replace 'oldString' with the string you want to replace, and ‘newString’ with the string, you want to use as a replacement.

Step 3: Write the Updated Content Back to the File

After replacing the string, you need to write the updated content back to the file. You can use the Set-Content cmdlet for this purpose.

$updatedContent | Set-Content -Path "C:\FilePath\file.txt"

Ensure that the path matches the file you read in Step 1.

Complete Example PowerShell Script

Here’s a complete example script that replaces the string “Hello” with “Hi” in a text file named “example.txt”:

# Define the path to the text file
$filePath = "C:\MyFolder\example.txt"

# Read the content of the file
$content = Get-Content -Path $filePath

# Replace "Hello" with "Hi"
$updatedContent = $content -replace 'Hello', 'Hi'

# Write the updated content back to the file
$updatedContent | Set-Content -Path $filePath

Notes

  • The -replace operator uses regular expressions by default, so make sure to escape any special regex characters if they are part of the string you want to replace.
  • If you want to replace multiple different strings, you can chain -replace operations.
  • The Get-Content and Set-Content cmdlets may not work well with very large files due to memory constraints. In such cases, consider using the .NET class [System.IO.File]::ReadAllText and [System.IO.File]::WriteAllText methods.

Replace a string in multiple files with PowerShell

You might also get requirements where we need to replace a string in multiple files. We can do this easily with PowerShell, which allows us to replace a string in multiple files with PowerShell.

To replace a string in multiple files using PowerShell, you can combine Get-ChildItem to retrieve the files, a loop to iterate through them, and -replace to perform the substitution. Here’s a simple example that replaces the string “oldText” with “newText” in all .txt files within a specified directory:

# Define the directory path and file pattern
$directoryPath = "C:\FilePath\files"
$filePattern = "*.txt"

# Retrieve all .txt files in the directory
$files = Get-ChildItem -Path $directoryPath -Filter $filePattern

# Loop through each file and replace the string
foreach ($file in $files) {
    (Get-Content $file.FullName) -replace 'oldText', 'newText' | Set-Content $file.FullName
}

Make sure to replace C:\FilePath\files with the actual directory path and adjust the strings ‘oldText’ and ‘newText’ to match your needs. Once you execute the script, it will update every .txt file in the specified directory, replacing all instances of “oldText” with “newText” using PowerShell.

Replace string in all files with PowerShell

To replace a string in all files within a directory and its subdirectories using PowerShell, you can use the Get-ChildItem cmdlet with the -Recurse parameter without specifying a file type filter. Here’s a simple example that replaces the string “oldText” with “newText” in all files within a directory and its subdirectories:

# Define the root directory path from where the recursion will start
$rootPath = "C:\path\to\your\root\directory"

# Retrieve all files in the directory and its subdirectories
$files = Get-ChildItem -Path $rootPath -Recurse | Where-Object {!$_.PSIsContainer}

# Loop through each file and replace the string
foreach ($file in $files) {
    $content = Get-Content $file.FullName -Raw
    $updatedContent = $content -replace 'oldText', 'newText'
    $updatedContent | Set-Content $file.FullName
}

Make sure to replace C:\path\to\your\root\directory with the actual path where you want to start the search, and adjust ‘oldText’ and ‘newText’ as needed. The Where-Object {!$_.PSIsContainer} part ensures that only files (not directories) are processed. This script will find all files in the specified root directory and its subdirectories, replacing every occurrence of “oldText” with “newText” in those files.

Replace string in files recursively using PowerShell

To replace a string in files recursively using PowerShell, which means it will include all files in the specified directory and its subdirectories, you can use the Get-ChildItem cmdlet with the -Recurse parameter. Here’s a simple example that replaces the string “oldText” with “newText” in all .txt files within a directory and its subdirectories:

# Define the root directory path from where the recursion will start
$rootPath = "C:\path\to\your\root\directory"
$filePattern = "*.txt" # The file type you want to search for

# Retrieve all .txt files in the directory and its subdirectories
$files = Get-ChildItem -Path $rootPath -Filter $filePattern -Recurse

# Loop through each file and replace the string
foreach ($file in $files) {
    (Get-Content $file.FullName) -replace 'oldText', 'newText' | Set-Content $file.FullName
}

Make sure to replace C:\path\to\your\root\directory with the actual path where you want to start the search, and adjust ‘oldText’ and ‘newText’ as needed. This script will find all .txt files in the specified root directory and its subdirectories, replacing every occurrence of “oldText” with “newText” in those files.

Conclusion

This tutorial provides a complete understanding of how to replace text in a file using PowerShell. Also, we covered how to replace a string in multiple files with PowerShell.

  • How to replace string in multiple files with PowerShell
  • Replace string in files recursively with PowerShell
  • Replace string in all files with PowerShell

You may also like:

>