Using the PowerShell replace() method or PowerShell replace operator, we can replace strings in multiple files.
Using the Get-ChildItem cmdlet gets the item from the specified location and piped the output to the Get-Content to read the file, we can call replace method to replace strings in multiple files.
In this article, we will discuss how to replace strings in multiple files using the PowerShell replace() method or replace operator.
Replace String in Multiple Files using replace() method
Using the PowerShell replace() method over the multiple files retrieved using the Get-ChildItem cmdlet, it can perform replace string in multiple files.
Let’s say, we have multiple config files in the directory and want to replace the copyright year in each of the config files with the current year.
To replace string in multiple files, we will have to get the files from the location and use the Get-Content cmdlet to read the content of the file.
$filePath = "D:\ps\Config\*.config" # Get the files from the folder and iterate using Foreach Get-ChildItem $filePath -Recurse | ForEach-Object { # Read the file and use replace() (Get-Content $_).Replace('2015','2022') | Set-Content $_ }
In the above PowerShell script, the $filePath variable contains the config files directory path.
Using the Get-ChildItem cmdlet, it retrieves the files recursively and uses the ForEach-Object to iterate over each file.
The Get-Content cmdlet uses the file path to read the content of the file and invoke replace() method with two arguments; old year to find and string value to replace with found text and piped the output to the Set-Content cmdlet
The Set-Content cmdlet uses the file path to save the file.
Let’s use the Get-Content to read the file content from one of the config files to check if replace string in multiple files is saved or not.
Get-Content -Path D:\PS\Config\app-1.config
PS C:\> Get-Content -Path D:\PS\Config\app-1.config
[assembly: AssemblyTitle("Logic")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Logic")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
PS C:\>
PowerShell Replace String in Multiple Files using replace Operator
We can use PowerShell replace operator to replace string in multiple files.
Let’s use the above example to replace strings in multiple config files to update the copyright year.
$filePath = "D:\ps\Config\*.config" # Get the files from the folder and iterate using Foreach Get-ChildItem $filePath -Recurse | ForEach-Object { # Read the file and use replace() (Get-Content $_) -replace '2015','2022' }
In the above PowerShell script, the $filePath variable stores the multiple config files directory path. Using the Get-ChildItem uses the $filePath to get the files and uses the ForEach-Object to iterate the file.
The Get-Content cmdlet uses the file path to read the content of the file and uses replace operator to replace the string in files.
Conclusion
I hope the above article on how to replace string in multiple files using the PowerShell replace() method or replace operator is helpful to you.
PowerShell string has built-in functions for string manipulation.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.