- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find and replace the word in a text file using PowerShell?
To search for the word in PowerShell and replace it in the file we will use the string operation. In fact, the Get-Content command in PowerShell is used to read almost any type of file content. In this article, we are considering one text file as shown below.
Get-Content C:\Temp\TestFile.txt
Output
PS C:\> Get-Content C:\Temp\TestFile.txt # In case of linux, networkInterface names are of the form eth* # In Windows, please use the network full name from Device Manager networkInterfaces: ["Microsoft Hyper-V Network Adapter" ] overrideMetricsUsingScriptFile: false scriptTimeoutInSec: 60 scriptFiles: - osType: windows filePath: monitors/NetworkMonitor/scripts/windows-metrics.bat - osType: unixBase filePath: monitors/NetworkMonitor/scripts/unix-base-metrics.sh
The above output is the content of the file and we are going to work with it. We need to find the NetWorkMonitor word and replace it with “Active”. Please note the above file is the text file so we don’t need to convert it to the string but if there are any other extensions you may need to convert the output to a string using .ToString() command or Out-String method before performing any operation.
Now, we will find the requested string first, and let’s check how many lines we need to replace using Select-String cmdlet.
(Get-Content C:\Temp\TestFile.txt) | Select-String -Pattern "NetworkMonitor"
Output
PS C:\> (Get-Content C:\Temp\TestFile.txt) | Select-String -Pattern "NetworkMonitor" filePath: monitors/NetworkMonitor/scripts/windows-metrics.bat filePath: monitors/NetworkMonitor/scripts/unix-base-metrics.sh
We found out there are two lines that have the “NetworkMonitor” word. We need to replace that word with the following command. You can also store Get-Content output in a variable and perform operation on it.
(Get-Content C:\Temp\TestFile.txt) -replace "NetworkMonitor","Active"
Output
PS C:\> (Get-Content C:\Temp\TestFile.txt) -replace "NetworkMonitor","Active" # In case of linux, networkInterface names are of the form eth* # In Windows, please use the network full name from Device Manager networkInterfaces: ["Microsoft Hyper-V Network Adapter" ] overrideMetricsUsingScriptFile: false scriptTimeoutInSec: 60 scriptFiles: - osType: windows filePath: monitors/Active/scripts/windows-metrics.bat - osType: unixBase filePath: monitors/Active/scripts/unix-base-metrics.sh
The above command replaced the file content but it is not stored yet so to save the updated file, we will use the Set-Content and the final code would be,
(Get-Content C:\Temp\TestFile.txt) -replace "NetworkMonitor","Active" | Set-Content C:\Temp\Testfile.txt -Verbos
You can also choose a different path to save the file. If the file is read-only then use -Force parameter with Set-Content cmdlet.
- Related Articles
- How to find and replace within a text file using Python?
- How to Find the Most Repeated Word in a Text File using Python?
- How to search and replace text in a file using Python?
- How to Find Line Number of a Given Word in text file using Python?
- How to grep and replace a word in a file on Linux?
- How to Count Word Occurrences in a Text File using Shell Script?
- C program to Replace a word in a text by another given word
- Find and Replace text in the entire table using a MySQL?
- How to find and replace text in Oracle?
- How to Find the Shortest Words in a Text File using Python?
- How to Find the Longest Words in a Text File using Python?
- How to find the file modified after a certain date using PowerShell?
- How to search in the file using PowerShell?
- How to replace string in a large one line, text file in Linux?
- How to find a replace a word in a string in C#?
