Replace Text From File in Batch Script
- Use Batch Script To Replace Text From the File
- Use Windows PowerShell To Replace Text From the File
- Conclusion

In this article, we will introduce some methods through which you can replace texts in a file. We are going to see two different ways.
Our first method contains only the batch script to perform the task and the second method provides the solution by Windows PowerShell.
Let’s consider a scenario where you’re working on a Windows server with restricted external internet access and software installation.
You’ve been assigned the task of removing all occurrences of specific data in TXT files. Imagine doing this manually for large files—it’s both tedious and inefficient.
Here’s where our methods come into play, enabling you to achieve your goal using only the tools available on the Windows machine—CMD and PowerShell.
Suppose we have a text file with the content below.
Test.txt
:
This is text that is saved in a text file. This is an update. ,,,,,,,,,,,,
We are going to replace these commands with empty characters.
Use Batch Script To Replace Text From the File
Batch Script:
@echo off
FOR /f "tokens=*" %%s IN (Test.txt) DO (
SET Texts=%%s
)
set Texts=%Texts:,=%
FOR /F "tokens=* delims=" %%x IN (Test.txt) DO SET text=%%x
ECHO %Texts% > "G:\BATCH\Test.txt" :: the path location of the txt file
We first read the file using the line FOR /f "tokens=*" %%s IN (Test.txt) DO (
and then through the line SET Texts=%%s
, we initialize a string variable with the texts of the file. We replaced each comma with an empty character through the line set Texts=%Texts:,=%
.
Lastly, we put the text into the file again. When we run the code above, we will see changes in our file content below.
This is text that is saved in a text file. This is an update.
Use Windows PowerShell To Replace Text From the File
This method will also provide the same result as our previous method did. In this method, we used PowerShell in our batch script. The example code for this method will look like this,
powershell -Command "(gc Test.txt) -replace ',', '' | Out-File -encoding ASCII Test.txt"
When we run the code, we’ll see changes in our file content below.
This is text that is saved in a text file. This is an update.
In this method, we leverage PowerShell’s -replace
operator to replace text. The command reads the content of the file (Test.txt
), replaces commas with empty characters, and then saves the updated content to Test.txt
.
Conclusion
By embracing batch scripting and Windows PowerShell, you empower yourself to automate text replacement tasks efficiently.
These methods serve as valuable tools for scenarios where manual editing is impractical due to file size or restricted access.
Whether you’re navigating large files or working within constrained environments, these techniques offer streamlined solutions.
Through the power of batch scripting and PowerShell, you can enhance your productivity and efficiency when handling text replacement challenges.
Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.
LinkedIn