Using the-replace operator
test.txt File contents
1234
Hello World
AAA "HHH"
Fwdbz
Test.ps1 script File
$ (get-content E:\demo\test.txt) |
Foreach-object {$_-replace ("1234", "5678")} |
Foreach-object {$_-replace ("Hello World", "Hello java")} |
Out-file E:\demo\test.txt
This is a simple string substitution, replacing 1234 in the Test.txt file with 5678, "Hello World" replaced by "Hello Java"
Above is a parameterless script execution that modifies the string in the file
The following is a string in the Pass parameter modification file
Test1.ps1 script File
#执行操作 TEST1.PS1 1234 5678 parameters are separated by a space, and the result of the operation is to replace 1234 in the Test.txt file with 5678
#输出参数1, 2
Write-host "$ ($args [0])" "$ ($args [2])"
$p 1 = "$ ($args [0])"
$p 2 = "$ ($args [1])"
$ (get-content E:\demo\test.txt) |
Foreach-object {$_-replace ($p 1, $p 2)} |
Out-file E:\demo\test.txt
PowerShell modifies a string in a file (-replace operator)