Replace entire line in INI file using Powershell

I have a problem replacing an entire line in an ini file, it just adds my result to the same line.

Here is the ini file:

[environment] APP_USER=Domain\User1 

I just want to replace APP_USER = Domain \ User1, for example, 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 code above:

  [environment] APP_USER=Domain\User2Domain\User1 

Help would be greatly appreciated.

// Regard PMS

+7
source share
1 answer

To fit the entire line:

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

.+ will match the rest of the line.

+18
source

All Articles