To demonstrate one method I use let first create the xml file in your example:
Define a Variable for the XML Filename
$xmlFile = "C:\temp\myconfig.xml"
Define an XML String to save to the file
$xmlFromString = [xml]@"
<server>
<service>
<Connector port="8080" password="password1234"/>
</service>
</server>
"@
Save the xml contents to the file
$xmlFromString.Save($xmlFile)
Resulting file content
Get-Content -Path $xmlFile
<server>
<service>
<Connector port="8080" password="password1234" />
</service>
</server>
Here is the PowerShell code to change the values
Get XML content from file
$xml = [xml](Get-Content -Path $xmlFile)
Finds the Element / Node and change the attribute values
$node = $xml.selectSingleNode('//server/service/Connector')
$node.port = "9090"
$node.password = "MyNewPassord4321"
Save the XML Contents Back out
$xml.Save($xmlFile)
Results
Get-Content -Path $xmlFile
<server>
<service>
<Connector port="9090" password="MyNewPassord4321" />
</service>
</server>
Save the commands to a PowerShell ps1 file and execute/run it via PowerShell.
We'll need additional details on what exactly your trying to accomplish such as:
- What rights will the user / account running the script has?
- Where will the script be running from? Local PC or server?
- One or multiple servers/work stations?
- Executed via Windows Scheduler Task?
Hope that was helpful. - Brooks
xmlpeople will want you to installxmlstarlet, but then you'd have to learn that too. Search/Replace for password1234 sounds like the "good enough" approach for this problem. Good luck.search replace [powershell]. I would expect quite a few solutions and I'm surprised that no PS users have offered some ideas. Read thru a few highly rated PS (with XML) question/questions and see if there is any additional info you need to add to make your question stand out. Good luck.