Replace a string in an .ini file RRS feed

  • Question

  • Hi,

    I have a simple .ini file which contains a line as follows:


    I simple want to loop through the file looking for the above line and change it to the following:


    I'e written the following but my skills in Regex don't seem to be working.  IS there an easy way to do this without Regex?  or can someone help me out with the simplest regex?

    $wordtoFind = ""
    $wordToReplace = "Server.domain.com"

    $wordtoFind = ""
    $wordToReplace = "Server.domain.com"
    #Check if the file has already been edited
    if (!(Test-Path $inibkp -PathType leaf))
    #Copy old config file
    Copy-Item $ini -Destination $inibkp
    #If it hasn't already been edited, make the changes.
    If(($a = Get-Content $ini).Contains($wordToFind)){
        $a | ForEach-Object { $_ -replace $wordToFind , $wordToReplace } | Set-Content $ini; write-host "Found + $wordtoFind + Replaced with $wordToReplace"
      write-host $wordToFind $wordToReplace

    Alter De Ruine

    Wednesday, February 19, 2020 11:22 AM

All replies