使用-replace操作符
test.txt文件内容
1234
hello world
aaa "hhh"
fwdbz
test.ps1脚本文件
$(Get-Content E:\demo\test.txt) |
Foreach-Object {$_ -replace ("1234","5678")} |
Foreach-Object {$_ -replace ("hello world","hello java")} |
Out-File E:\demo\test.txt
这是个简单的字符串替换,将test.txt文件中的1234替换成5678,"hello world"替换成"hello java"
上面是无参数脚本执行,修改文件中字符串
下面这个是传递参数修改文件中字符串
test1.ps1脚本文件
#执行操作 test1.ps1 1234 5678参数之间通过空格隔开,操作结果如下将test.txt文件中的1234 替换为 5678
#输出参数1,2
Write-Host "$($args[0])" "$($args[2])"
$p1 = "$($args[0])"
$p2 = "$($args[1])"
$(Get-Content E:\demo\test.txt) |
Foreach-Object {$_ -replace ($p1,$p2)} |
Out-File E:\demo\test.txt
原文地址:https://www.cnblogs.com/tracy123/p/8439347.html
时间: 2024-10-10 04:39:45