This blog will discuss methods to fix the mentioned query.
How to Read Text Files and Replace Text Using PowerShell?
Now, we will discuss the two procedures to read text files and replace text separately, and in combination.
Method 1: Read Text Files Using PowerShell “Get-Content” Command
The “Get-Content” cmdlet is used in PowerShell to read text files. This cmdlet displays text file data within the PowerShell console.
Example 1: Read a Single Text File
This example will demonstrate how to read text files using the “Get-Content” cmdlet. But, let’s first, create a text file using the “Out-File” cmdlet.
According to the above code:
- First, add the string within the inverted command.
- After that, add a pipeline “|” to transfer the result of the previous command to the next command.
- Then, use the “Out-File” cmdlet along with the file path to export the output to a text file:
Let’s read the content of the exported text file using the “Get-Content” cmdlet with the file path:
A single text file was read successfully.
Example 2: Read All the Text Files Inside a Specified Directory
This demonstration will help to read all the text files available in the respective directory:
First add the “Get-Content” cmdlet along with the folder address and add the wildcard “*” along with “.txt” extension to read all the text files in the respective folder:
All the text files were read inside a directory successfully.
Method 2: Replace Text Using PowerShell “-replace” Parameter
The “-replace” parameter is used to replace text within a string. It takes two words instances separated by a comma. This option works in such a way that it searches the first word and replaces it with the second word.
Example 1: Replace Text Within a String
Now, replace the text instances within a string assigned variable:
$str -replace "Earth", "Mars"
According to the above code:
- First, add a variable and assign a text string to it.
- After that, in the next line, specify the variable and the “-replace” operator along with the two words separated by commas.
- The first word will be searched inside a string and replaced with the second one:
It can be observed that “Earth” has been replaced by “Mars”.
Example 2: Replace All the Text Instances Inside a File
This example will demonstrate to replace all the text instances inside the specified text file:
According to the above code:
- First, write the “Get-Content” cmdlet along with the file address within small brackets.
- After that, add the “-replace” parameter and add two words within inverted commas separated by a comma.
- Then, add a pipeline “|” and use the “Set-Content” alongside the target file path:
Let’s check the replaced text by executing the below command:
It can be observed that the various text instances inside a file were successfully replaced.
Conclusion
PowerShell uses the “Get-Content” cmdlet along with the file path to read text files. In order to replace text, first add the string or file path with “Get-Content” and then add the “-replace” parameter alongside two words separated by a comma. This post has elaborated a detailed guide to read and replace text in PowerShell separately, or simultaneously.