Find the key and replace its respective value in config/xml files using powershell












0















I have an config file similar to below:



<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.3" newVersion="4.1.1.3" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<appSettings>
<add key="key1" value="Value1" />
<add key="key2" value="Value12" />
<add key="key3" value="Value3" />
</appSettings>
</configuration>


Now through powershell I'm trying to replace some values, say for example Value1. For that I have written the below script :



$original_file = "C:test.xml"
(Get-Content $original_file) | Foreach-Object {
$_ -replace 'Value1', 'Newvalue'
} | Set-Content $original_file


So what it does it replace all Value1 strings with Newvalue string. The problem I'm facing here is it changing all the values whereever Value1 is found, like this.



  <appSettings>
<add key="key1" value="Newvalue" />
<add key="key2" value="Newvalue2" /> --- this is not supposed to happen
<add key="key3" value="Value3" />
</appSettings>


And also in reality, I actual values are very long strings.



So is there any way I can find the key and change its respective value? Like find Key1 and change its value to NewValue.



Any help is highly appreciated.










share|improve this question

























  • if your XML is loaded into $InStuff, then this will address the array that has the add items in it ... $InStuff.configuration.appSettings.add.Where({$_.Key -eq 'key1'}) ... and get the array item that has the XmlElement with a key1 in it.

    – Lee_Dailey
    Nov 20 '18 at 5:19


















0















I have an config file similar to below:



<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.3" newVersion="4.1.1.3" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<appSettings>
<add key="key1" value="Value1" />
<add key="key2" value="Value12" />
<add key="key3" value="Value3" />
</appSettings>
</configuration>


Now through powershell I'm trying to replace some values, say for example Value1. For that I have written the below script :



$original_file = "C:test.xml"
(Get-Content $original_file) | Foreach-Object {
$_ -replace 'Value1', 'Newvalue'
} | Set-Content $original_file


So what it does it replace all Value1 strings with Newvalue string. The problem I'm facing here is it changing all the values whereever Value1 is found, like this.



  <appSettings>
<add key="key1" value="Newvalue" />
<add key="key2" value="Newvalue2" /> --- this is not supposed to happen
<add key="key3" value="Value3" />
</appSettings>


And also in reality, I actual values are very long strings.



So is there any way I can find the key and change its respective value? Like find Key1 and change its value to NewValue.



Any help is highly appreciated.










share|improve this question

























  • if your XML is loaded into $InStuff, then this will address the array that has the add items in it ... $InStuff.configuration.appSettings.add.Where({$_.Key -eq 'key1'}) ... and get the array item that has the XmlElement with a key1 in it.

    – Lee_Dailey
    Nov 20 '18 at 5:19
















0












0








0








I have an config file similar to below:



<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.3" newVersion="4.1.1.3" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<appSettings>
<add key="key1" value="Value1" />
<add key="key2" value="Value12" />
<add key="key3" value="Value3" />
</appSettings>
</configuration>


Now through powershell I'm trying to replace some values, say for example Value1. For that I have written the below script :



$original_file = "C:test.xml"
(Get-Content $original_file) | Foreach-Object {
$_ -replace 'Value1', 'Newvalue'
} | Set-Content $original_file


So what it does it replace all Value1 strings with Newvalue string. The problem I'm facing here is it changing all the values whereever Value1 is found, like this.



  <appSettings>
<add key="key1" value="Newvalue" />
<add key="key2" value="Newvalue2" /> --- this is not supposed to happen
<add key="key3" value="Value3" />
</appSettings>


And also in reality, I actual values are very long strings.



So is there any way I can find the key and change its respective value? Like find Key1 and change its value to NewValue.



Any help is highly appreciated.










share|improve this question
















I have an config file similar to below:



<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.3" newVersion="4.1.1.3" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<appSettings>
<add key="key1" value="Value1" />
<add key="key2" value="Value12" />
<add key="key3" value="Value3" />
</appSettings>
</configuration>


Now through powershell I'm trying to replace some values, say for example Value1. For that I have written the below script :



$original_file = "C:test.xml"
(Get-Content $original_file) | Foreach-Object {
$_ -replace 'Value1', 'Newvalue'
} | Set-Content $original_file


So what it does it replace all Value1 strings with Newvalue string. The problem I'm facing here is it changing all the values whereever Value1 is found, like this.



  <appSettings>
<add key="key1" value="Newvalue" />
<add key="key2" value="Newvalue2" /> --- this is not supposed to happen
<add key="key3" value="Value3" />
</appSettings>


And also in reality, I actual values are very long strings.



So is there any way I can find the key and change its respective value? Like find Key1 and change its value to NewValue.



Any help is highly appreciated.







xml powershell app-config powershell-v1.0






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 5:12







CrazyCoder

















asked Nov 20 '18 at 4:53









CrazyCoderCrazyCoder

417116




417116













  • if your XML is loaded into $InStuff, then this will address the array that has the add items in it ... $InStuff.configuration.appSettings.add.Where({$_.Key -eq 'key1'}) ... and get the array item that has the XmlElement with a key1 in it.

    – Lee_Dailey
    Nov 20 '18 at 5:19





















  • if your XML is loaded into $InStuff, then this will address the array that has the add items in it ... $InStuff.configuration.appSettings.add.Where({$_.Key -eq 'key1'}) ... and get the array item that has the XmlElement with a key1 in it.

    – Lee_Dailey
    Nov 20 '18 at 5:19



















if your XML is loaded into $InStuff, then this will address the array that has the add items in it ... $InStuff.configuration.appSettings.add.Where({$_.Key -eq 'key1'}) ... and get the array item that has the XmlElement with a key1 in it.

– Lee_Dailey
Nov 20 '18 at 5:19







if your XML is loaded into $InStuff, then this will address the array that has the add items in it ... $InStuff.configuration.appSettings.add.Where({$_.Key -eq 'key1'}) ... and get the array item that has the XmlElement with a key1 in it.

– Lee_Dailey
Nov 20 '18 at 5:19














1 Answer
1






active

oldest

votes


















1














Don't use regex against structured markup - use XPath!



# Load the xml document
$filename = 'C:test.xml'
$xmlDoc = New-Object xml
$xmlDoc.Load($filename)

# Select all applicable nodes
$nodes = $xmlDoc.SelectNodes('//appSettings/add[@key="key1"]')

# In each node, replace the value `Value1` with `NewValue`
foreach($node in $nodes){
$node.value = $node.value.Replace('Value1','NewValue')
}

# Save the document
$xmlDoc.Save($filename)


The XPath expression //appSettings/add[@key="key1"] will select any add node which has an attribute named key with the value key1 and whose parent is an appSettings node.






share|improve this answer


























  • thanks for replying. This has solved my issue. One issue what I'm facing here is I'm changing the values continuously for every 3 hrs. So If I change the value first time Value1 to NewValue , Second time this doesn't work because there is no Value1 anymore. So is there any way I can directly replace the value for key1 without mentioning 'Value1' anywhere like in this $node.value.Replace('Value1','NewValue') ?

    – CrazyCoder
    Nov 20 '18 at 10:37











  • I understand that this requirement was not there in the original question , if you can help me that would be helpful.

    – CrazyCoder
    Nov 20 '18 at 10:38











  • @CrazyCoder just set $node.value to whatever it needs to be instead: $node.Value = "NewValue"

    – Mathias R. Jessen
    Nov 20 '18 at 10:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386446%2ffind-the-key-and-replace-its-respective-value-in-config-xml-files-using-powershe%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Don't use regex against structured markup - use XPath!



# Load the xml document
$filename = 'C:test.xml'
$xmlDoc = New-Object xml
$xmlDoc.Load($filename)

# Select all applicable nodes
$nodes = $xmlDoc.SelectNodes('//appSettings/add[@key="key1"]')

# In each node, replace the value `Value1` with `NewValue`
foreach($node in $nodes){
$node.value = $node.value.Replace('Value1','NewValue')
}

# Save the document
$xmlDoc.Save($filename)


The XPath expression //appSettings/add[@key="key1"] will select any add node which has an attribute named key with the value key1 and whose parent is an appSettings node.






share|improve this answer


























  • thanks for replying. This has solved my issue. One issue what I'm facing here is I'm changing the values continuously for every 3 hrs. So If I change the value first time Value1 to NewValue , Second time this doesn't work because there is no Value1 anymore. So is there any way I can directly replace the value for key1 without mentioning 'Value1' anywhere like in this $node.value.Replace('Value1','NewValue') ?

    – CrazyCoder
    Nov 20 '18 at 10:37











  • I understand that this requirement was not there in the original question , if you can help me that would be helpful.

    – CrazyCoder
    Nov 20 '18 at 10:38











  • @CrazyCoder just set $node.value to whatever it needs to be instead: $node.Value = "NewValue"

    – Mathias R. Jessen
    Nov 20 '18 at 10:57
















1














Don't use regex against structured markup - use XPath!



# Load the xml document
$filename = 'C:test.xml'
$xmlDoc = New-Object xml
$xmlDoc.Load($filename)

# Select all applicable nodes
$nodes = $xmlDoc.SelectNodes('//appSettings/add[@key="key1"]')

# In each node, replace the value `Value1` with `NewValue`
foreach($node in $nodes){
$node.value = $node.value.Replace('Value1','NewValue')
}

# Save the document
$xmlDoc.Save($filename)


The XPath expression //appSettings/add[@key="key1"] will select any add node which has an attribute named key with the value key1 and whose parent is an appSettings node.






share|improve this answer


























  • thanks for replying. This has solved my issue. One issue what I'm facing here is I'm changing the values continuously for every 3 hrs. So If I change the value first time Value1 to NewValue , Second time this doesn't work because there is no Value1 anymore. So is there any way I can directly replace the value for key1 without mentioning 'Value1' anywhere like in this $node.value.Replace('Value1','NewValue') ?

    – CrazyCoder
    Nov 20 '18 at 10:37











  • I understand that this requirement was not there in the original question , if you can help me that would be helpful.

    – CrazyCoder
    Nov 20 '18 at 10:38











  • @CrazyCoder just set $node.value to whatever it needs to be instead: $node.Value = "NewValue"

    – Mathias R. Jessen
    Nov 20 '18 at 10:57














1












1








1







Don't use regex against structured markup - use XPath!



# Load the xml document
$filename = 'C:test.xml'
$xmlDoc = New-Object xml
$xmlDoc.Load($filename)

# Select all applicable nodes
$nodes = $xmlDoc.SelectNodes('//appSettings/add[@key="key1"]')

# In each node, replace the value `Value1` with `NewValue`
foreach($node in $nodes){
$node.value = $node.value.Replace('Value1','NewValue')
}

# Save the document
$xmlDoc.Save($filename)


The XPath expression //appSettings/add[@key="key1"] will select any add node which has an attribute named key with the value key1 and whose parent is an appSettings node.






share|improve this answer















Don't use regex against structured markup - use XPath!



# Load the xml document
$filename = 'C:test.xml'
$xmlDoc = New-Object xml
$xmlDoc.Load($filename)

# Select all applicable nodes
$nodes = $xmlDoc.SelectNodes('//appSettings/add[@key="key1"]')

# In each node, replace the value `Value1` with `NewValue`
foreach($node in $nodes){
$node.value = $node.value.Replace('Value1','NewValue')
}

# Save the document
$xmlDoc.Save($filename)


The XPath expression //appSettings/add[@key="key1"] will select any add node which has an attribute named key with the value key1 and whose parent is an appSettings node.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 8:44

























answered Nov 20 '18 at 8:36









Mathias R. JessenMathias R. Jessen

56.9k458103




56.9k458103













  • thanks for replying. This has solved my issue. One issue what I'm facing here is I'm changing the values continuously for every 3 hrs. So If I change the value first time Value1 to NewValue , Second time this doesn't work because there is no Value1 anymore. So is there any way I can directly replace the value for key1 without mentioning 'Value1' anywhere like in this $node.value.Replace('Value1','NewValue') ?

    – CrazyCoder
    Nov 20 '18 at 10:37











  • I understand that this requirement was not there in the original question , if you can help me that would be helpful.

    – CrazyCoder
    Nov 20 '18 at 10:38











  • @CrazyCoder just set $node.value to whatever it needs to be instead: $node.Value = "NewValue"

    – Mathias R. Jessen
    Nov 20 '18 at 10:57



















  • thanks for replying. This has solved my issue. One issue what I'm facing here is I'm changing the values continuously for every 3 hrs. So If I change the value first time Value1 to NewValue , Second time this doesn't work because there is no Value1 anymore. So is there any way I can directly replace the value for key1 without mentioning 'Value1' anywhere like in this $node.value.Replace('Value1','NewValue') ?

    – CrazyCoder
    Nov 20 '18 at 10:37











  • I understand that this requirement was not there in the original question , if you can help me that would be helpful.

    – CrazyCoder
    Nov 20 '18 at 10:38











  • @CrazyCoder just set $node.value to whatever it needs to be instead: $node.Value = "NewValue"

    – Mathias R. Jessen
    Nov 20 '18 at 10:57

















thanks for replying. This has solved my issue. One issue what I'm facing here is I'm changing the values continuously for every 3 hrs. So If I change the value first time Value1 to NewValue , Second time this doesn't work because there is no Value1 anymore. So is there any way I can directly replace the value for key1 without mentioning 'Value1' anywhere like in this $node.value.Replace('Value1','NewValue') ?

– CrazyCoder
Nov 20 '18 at 10:37





thanks for replying. This has solved my issue. One issue what I'm facing here is I'm changing the values continuously for every 3 hrs. So If I change the value first time Value1 to NewValue , Second time this doesn't work because there is no Value1 anymore. So is there any way I can directly replace the value for key1 without mentioning 'Value1' anywhere like in this $node.value.Replace('Value1','NewValue') ?

– CrazyCoder
Nov 20 '18 at 10:37













I understand that this requirement was not there in the original question , if you can help me that would be helpful.

– CrazyCoder
Nov 20 '18 at 10:38





I understand that this requirement was not there in the original question , if you can help me that would be helpful.

– CrazyCoder
Nov 20 '18 at 10:38













@CrazyCoder just set $node.value to whatever it needs to be instead: $node.Value = "NewValue"

– Mathias R. Jessen
Nov 20 '18 at 10:57





@CrazyCoder just set $node.value to whatever it needs to be instead: $node.Value = "NewValue"

– Mathias R. Jessen
Nov 20 '18 at 10:57


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386446%2ffind-the-key-and-replace-its-respective-value-in-config-xml-files-using-powershe%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$