Update the properties file using powershell

advertisements

I want to update below property file contents using powershell and replace ${BuildPath} with location = D:\Data\Sample\lib

tibco.alias.junit.jar=${BuildPath}\\junit.jar
tibco.alias.TextDiff.jar=${BuildPath}\\TextDiff.jar
tibco.alias.XMLDiff.jar=${BuildPath}\\XMLDiff.jar

Could some one please help me logic on powershell to update this property file.

Thanks


So you have a file with those properties in it yes? Who's to say how it was created or if that is the only contents? Lets just assume you have a file with lots of properties other than that just in case. You want to navigate the file replacing all occurrences* of ${BuildPath} with D:\Data\Sample\lib

$replacePath = "D:\Data\Sample\lib"
$newFile = Get-Content c:\temp\file.ini | ForEach-Object{
    $_ -replace '\${BuildPath}',($replacePath -replace '\\',"\\")
}
$newFile | Set-Content c:\temp\file.ini

This would give the following output in c:\temp\file.ini

tibco.alias.junit.jar=D:\\Data\\Sample\\lib\\junit.jar
tibco.alias.TextDiff.jar=D:\\Data\\Sample\\lib\\TextDiff.jar
tibco.alias.XMLDiff.jar=D:\\Data\\Sample\\lib\\XMLDiff.jar

* regex will only replace the first occurrence on each line. Doubt there would be more than one anyway.

Welcome to SO

This community works alot better if you show some effort first. You question did not show that. I only saw a request for an answer. We are here to support programmers and programming enthusiasts. We are not a code writing service.