Working with XML files is a common task in the IT world. Whether you’re updating server names in a configuration file, modifying application settings, or adjusting deployment parameters, mastering XML manipulation using PowerShell can save you hours of manual editing. Instead of opening files in a text editor and risking syntax errors, you can safely automate changes in a structured and safer way.
In this article, we’ll explore how to read, modify, and save XML files using PowerShell.
The same technique is applicable not only to .xml files, but to any file format that uses a hierarchical tree structure composed of a root element that branches into child elements. Many configuration files used by Microsoft products, web applications, and enterprise systems rely on this structure.
Why use PowerShell for XML Manipulation?
PowerShell treats XML as a native object type, which makes it extremely easy to parse and modify structured data. Once loaded as an XML object, you can use built-in cmdlets and object properties to access its elements and attributes just like you would with any other PowerShell object.
This object-oriented approach is much safer and more reliable than performing text-based replacements.
Loading XML
The first step is to load the XML content into memory using the Get-Content cmdlet and cast it as an [xml] object:
[xml]$XMLContent = Get-Content -Path <path to the .xml file>By casting the content to [xml], PowerShell parses the file and creates an in-memory XML object that you can navigate.
Reading XML elements
Once the XML is loaded, you can access specific elements using dot notation.
Let’s suppose you’ve got the following XML:
<?xml version="1.0" encoding="utf-8"?>
<Data version="2.0">
<Servers>
<Server Name="PreProduction" Value="OldPreProductionServer" />
<Server Name="Production" Value="OldProductionServer" />
</Servers>
</Data>
To retrieve the Production element, you can use the following code:
$ProductionElement = $XMLContent.Data.Servers.Server |
where {$_.Name -eq “Production”}Here’s how it works:
• $XMLContent.Data accesses the root node.
• .Servers.Server retrieves all <Server> elements.
• Where filters the collection to find the one with Name=”Production”.
Updating XML elements
Once you have accessed the element you need, updating it is straightforward.
To replace OldProductionServer with a new value:
$ProductionElement.value = "NewProductionServer"Save the changes
After modifying the XML in memory, the final step is to save the changes back to a file.
$XMLContent.Save(<path to the .xml file>) You can overwrite the original file or save it as a new version. Saving to a new file first is often safer in production environments, as it allows you to validate the changes before replacing the original.
Conclusion
Updating XML files using PowerShell is efficient, reliable, and very easy. By loading the file as an XML object, navigating its structure with dot notation, modifying elements, and saving the result, you can fully automate all sorts of tasks you may need.


