Replace a whole line in a INI-file using Powershell
14,053
To match the whole line:
-replace "APP_USER=.+","APP_USER=$user"
The .+
will match the rest of the line.
Author by
user2400659
Updated on July 29, 2022Comments
-
user2400659 over 1 year
i have a problem with replacing a whole line in a ini-file, it just seems to add my result to thesame line.
Here is the ini-file:
[environment] APP_USER=Domain\User1
I just wish to replace the APP_USER=Domain\User1 with 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 above code:
[environment] APP_USER=Domain\User2Domain\User1
Help would be much appreciated.
//Regard PMS
-
user2400659 almost 11 yearsThanks alot, just what i needed.
-
Andy Arismendi almost 11 years@user2400659, good to hear, don't forget to mark the correct as appropriate.
-
Nimblejoe over 8 years.+ was what I needed. Spent some time looking for this one. Thanks!
-
Darrin over 6 years@user2400659, (or anyone) what if you want to remove the matched line, including the \n newline? This solution helped me blank out lines I want removed, based on whether they contain a string. But then I end up with hundreds of empty newlines in the file, scattered everywhere. :-(