用Powershell替换一个ini文件中的整行。

时间:2021-09-05 22:29:38

i have a problem with replacing a whole line in a ini-file, it just seems to add my result to thesame line.

我有一个问题,替换一个ini文件中的整个行,它似乎只是将我的结果添加到同一行。

Here is the ini-file:

这是ini文件:

    [environment]
APP_USER=Domain\User1

I just wish to replace the APP_USER=Domain\User1 with for example APP_USER=Domain\User2.

我只想用例如APP_USER=Domain\User1替换APP_USER=Domain\User2。

Here is my code:

这是我的代码:

$USER = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name          
(Get-Content D:\Test\test.ini) | ForEach-Object { $_ -replace "APP_USER=" , "APP_USER=$user" } | Set-Content D:\Test\test.ini

I get this result when i use the above code:

当我使用上述代码时,我得到了这个结果:

    [environment]
APP_USER=Domain\User2Domain\User1

Help would be much appreciated.

非常感谢你的帮助。

//Regard PMS

/ /将经前综合症

1 个解决方案

#1


18  

To match the whole line:

匹配整条线:

-replace "APP_USER=.+","APP_USER=$user"

The .+ will match the rest of the line.

这个。+将匹配剩下的行。

#1


18  

To match the whole line:

匹配整条线:

-replace "APP_USER=.+","APP_USER=$user"

The .+ will match the rest of the line.

这个。+将匹配剩下的行。