HomeHow ToPowerShell script for mass replacement of words in text

PowerShell script for bulk replacement of words in text

PowerShell script for bulk text editing: Text editors like Notepad and Notepad++ are used to create many different types of files such as log files, scripts, PowerShell files, and more. Since the text editor you are using can create these files, it can also edit them. But if you have many text files and you need to replace a few words in all of them, then you can do it with a PowerShell script. The script will do it, without having to open each file individually to replace the word. Let's see how you can do it. [su_note note_color=”#ebebeb” text_color=”#271e45″ radius=”2″]You can use the same script for almost any other type of file that can be created with a text editor. [/su_note] First, you need to place all the text files you want to edit in the same folder. The script will only check one folder and not your entire system. We have mentioned the free text editor Notepad++ many times throughout this page . You can download and use the application. Create a new file, copy and paste the following text:PowerShell





Get-ChildItem 'folder-path\*.txt' -Recurse | ForEach { (Get-Content $_ | ForEach { $_ -replace 'original-word', 'new-word' }) | Set-Content $_ }

You will need to edit the above script. Change “folder-path” to the actual path to the folder containing the text files you gathered to edit.
Then you will need to replace ‘original-word’ with the word you want to replace. Finally, change “new-word” and add the word you want to replace the old one.
For example, I have some text files that have the word ‘Publish’ and I want to replace the word publish with the word article. Here is how I converted the above script:

 Get-ChildItem 'C:\Users\iguru\Desktop\files\*.txt' -Recurse | ForEach { (Get-Content $_ | ForEach { $_ -replace 'post', 'article' }) | Set-Content $_ }

Once you are done with the conversion, save your text with the ps1 file extension. In Notepad++ before saving, make sure you have changed the file type. Run the script and your files will be changed.
If you want to use the same script for files other than .txt files, change the file extension in the first line. For example,
C:\Users\iguru\Desktop\files\*.txt
should become
C:\Users\iguru\Desktop\files\*.xml
[su_note note_color=”#ebebeb” text_color=”#271e45″ radius=”2″]Warning: The script does not recognize words. If you want to replace the word the with a, it replaces 'the' wherever it finds it. Let's say at the beginning of the word theme.
[/su_note]
_____________________________________________

📧
Subscribe to the SecNews Newsletter

The most important Security & Technology news in your Inbox.

SecNews
SecNewshttps://www.secnews.gr
In a world without fences and walls, who needs Gates and Windows

SEARCH

FOLLOW US

📧
Newsletter SecNews
The most important Security & Technology news in your inbox.

LIVE NEWS