Can PowerShell handle XML files?
Extensible Markup Language is just text, so PowerShell can certainly load the contents of an XML file into a variable:
$xml = Get-Content MyXML.xml
But that doesn't tell PowerShell to actually understand the XML; it's just a big wodge of text. To have PowerShell read the XML, you can force it to parse the XML:
[xml]$xml = Get-Content MyXML.xml
Doing so will run the XML through the .NET Framework's XmlDocument class, meaning it will examine the XML and build a document object model from it. If the XML doesn't parse or validate, then you'll get an error.
The resulting XML object essentially turns XML nodes such as <computer></computer> into objects and collections, and it lets you access attributes like<tag attribute="value"> as object properties. It can get fairly complicated quickly. You also obtain ways to add nodes and attributes, remove them and write the modified bit back to a text file.
There are also methods for executing XPath queries. XPath is a XML query language that lets you search an XML file for specified nodes, attributes and so on.
About the author
Don Jones is a well-known and respected PowerShell expert and educator. He's co-author of three books on PowerShell (see PowerShellBooks.com for a list). You can find his content online, including his PowerShell Q&A forums, by visiting DonJones.com.
Dig Deeper on Windows PowerShell Scripting
Related Q&A from Don Jones
If you don't need a GUI display, PowerShell can be hosted in several hosting applications so you can use cmdlets instead of a graphical input prompt.continue reading
With some simple configuration, admins can run a PowerShell script each time a computer starts with the help of the Task Scheduler GUI and cmdlets.continue reading
Each PowerShell version has different features made useful by add-ins and snap-ins, so admins have to pay attention to what features work where.continue reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.