locked
Replace a string in an .ini file RRS feed

  • Question

  • Hi,

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

    PATH_MAIN=\\192.168.1.1\testapp

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

    PATH_MAIN=\\Server.domain.com\testapp

    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 = "192.168.1.1"
    $wordToReplace = "Server.domain.com"

    $wordtoFind = "192.168.1.1"
    $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