使用powershell脚本替换单词

时间:2015-03-02 05:27:56

标签: windows powershell

我有一个用于设置环境变量的文本文件。我需要更改' config'的值变量。以下是文件内容

env.mode:on config:env
服务器:2
db:mssql
.........
.........

env值可以是任何东西。它应该被替换为prod。 (config:prod)。我知道我们可以用powershell get-content替换这个词。由于word不是静态的,需要使用Powershell设置值。有人能帮我吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

$file = 'c:\myconfig.config'
$configValue = 'prod'
(Get-Content $file) -replace '^config:.*', "config: $configValue" | Set-Content $file