Reputation: 13
Hello I am trying to run a script in PowerShell ISE to replace a line of text in an .ini file.
(Get-Content C:\ProgramData\Nuance\NaturallySpeaking12\nssystem.ini) | ForEach-Object { $_ -replace "Default NAS Address=","Default NAS Address=BHPAPPDGN01V" } | Set-Content C:\ProgramData\Nuance\NaturallySpeaking12\nssystem.ini
This script works BUT when I run it multiple times the .ini text keeps getting added to the end of the line giving me a bunch of junk.
Example: Ran script 4 times "Default NAS Address=BHPAPPDGN01VBHPAPPDGN01VBHPAPPDGN01VBHPAPPDGN01V"
Upvotes: 1
Views: 5659
Reputation: 339
I wasn't keen on the suggestions above so here's my solution in case anyone stumbles in from Google and finds it useful.
Contents of test.ini
[Other Parameter] = Something
[My Parameter] = What I wouldnt give for a change of heart
[Other Parameter] = Nothing
My solution:
$MyString = Get-Content "C:\Test_Script\test.ini"
$NewString = $MyString.replace("[My Parameter] = What I wouldnt give for a change of heart","[My Parameter] = I gave my heart for a piece of string")
Set-Content -Path "C:\Test_Script\test.ini" -Value $NewString
Changed contents of test.ini
[Other Parameter] = Something
[My Parameter] = I gave my heart for a piece of string
[Other Parameter] = Nothing
Upvotes: 0
Reputation: 1
replace
"-replace "Default NAS Address="
to
"-replace "Default NAS Address=[A-Z0-9]*"
Upvotes: 0
Reputation: 72630
Try this :
(Get-Content C:\ProgramData\Nuance\NaturallySpeaking12\nssystem.ini) | ForEach-Object { $_ -replace 'Default NAS Address=.*','Default NAS Address=BHPAPPDGN01V' } | Set-Content C:\ProgramData\Nuance\NaturallySpeaking12\nssystem.ini
I just try to use a regex to select evrything behind the =
.
Upvotes: 4