首页 > 解决方案 > Powershell 搜索特定字符串,然后在其后添加文本

问题描述

我很确定这很容易做到,但我找不到答案。假设我有一个包含一堆行的文本文件。下面的文本文件的内容

dbuser=admin
dbpassword=

所以在Powershell中我想找到字符串“dbpassword =”并在=之后添加文本“password”。我搜索过的解决方案有 -replace 但我不想替换该行,只想向其中添加“密码”。

任何人都可以分享 Powershell 代码可能是什么吗?

标签: powershell

解决方案


感谢大家的贡献,我最终找到了这个解决方案。

$config = Get-Content path\to\file\somefile.txt
$password = abcdef
$config -replace "dbpassword=.*",("dbpassword="+$password) |
    Set-Content path\to\file\somefile.txt

推荐阅读