PowerShell is powerful enough to perform the File Explorer operations, such as copying, moving, renaming, or deleting a file. It can read and replace the content inside a text file, whether a string or a single word. Particularly, PowerShell has such commands that can be employed to read and replace every instance of a string.
This tutorial will demonstrate a couple of methods to resolve the mentioned query.
How to Replace Every Instance of a String in a File by Utilizing PowerShell?
These approaches can be employed to fix the stated query:
Method 1: Utilize the “Get-Content” and “Set-Content” Methods in Order to Replace Each Instance of a String in a Document File
This approach first uses the “Get-Content” cmdlet to retrieve the data inside the file and then uses the “Set-Content” cmdlet to replace or write the new occurrence or content. Particularly, the “-Replace” parameter is used to replace the instance of a string in a file.
Take a look at the given example to get a thorough understanding of it.
Example
First of all, let’s check the file version before replacing every occurrence. For that reason, execute the “Get-Content” cmdlet alongside the file path:
Now, let’s replace the word “Cat” with the “Dog”:
According to the above command:
- Firstly, we used the “Get-Content” cmdlet alongside the file path within the inverted command.
- Next, we added the “-Replace” parameter and specified two words separated by a comma, where the first word will be replaced with the second one.
- Then, the pipeline “|” is utilized to transfer the output to the “Set-Content” cmdlet as an input.
- Finally, this cmdlet is used alongside the file path to confirm the replacement of the string in a file:
For the verification, again, get the content of the selected file:
It can be observed that the word “Cat” has been replaced with “Dog” successfully.
Method 2: Use the “System.IO.File” Method to Replace Each Occurrence/Instance of a String in a File
Microsoft’s .Net class “File” is specifically used to manage the File Explorer. Particularly, it plays a vital role in replacing the instance of a string in a file.
Example
Firstly, check out the content of the “php.txt”:
Then, run the following commands:
> [System.IO.File]::WriteAllText("C:\Doc\php.txt", $str)
According to the above code:
- First of all, we created a variable and then specified the “IO.File” class.
- The “ReadAllText()” method of the mentioned class reads the text in the file, while the “Replace()” method replaces the selected occurrences in the text file.
- In the second line, we have used the “[System.IO.File]::WriteAllText()” method and added the file path and then variable to replace the specified occurrence in the file:
Let’s check whether the word has been replaced or not. For
The word “php” has been replaced with “Python” successfully.
Conclusion
With PowerShell, every occurrence/instance of a string in a file can be replaced using two methods, including “Get-Content” and “Set-Content” and “System.IO.File”. Both methods first retrieve the text file and then replace the specified word’s occurrence in that text file. This post has presented a thorough guide to resolve the stated problem.