Click here to Skip to main content
15,902,032 members
Articles / Programming Languages / PowerShell

PowerShell and XML

Rate me:
Please Sign up or sign in to vote.
4.75/5 (10 votes)
27 Feb 2010CPOL6 min read 276.7K   44   8
Introduction to how easy PowerShell makes working with XML programmatically


XML is everywhere. As a result, many of us find the need to work with XML and traditional text editors don't cut it. Some editors provide decent formatting (like Notepad++), but they don't provide a mechanism to examine the underlying data in the XML programmatically.

PowerShell makes handling XML very simple. It converts XML elements to properties on .NET objects without the need to write any parsing code. So all you need is PowerShell installed on your machine and you're ready to go!

A Quick Example

Here's a quick example to show how PowerShell maps the XML elements and attributes to object properties. It assumes the file.xml file exists and contains the text below:

<!-- file.xml -->
	<employee id="101">
		<name>Frankie Johnny</name>
	<employee id="102">
		<name>Elvis Presley</name>
	<employee id="301">
		<name>Ella Fitzgerald</name>

Examples of loading file.xml into an XmlDocument object and getting at the nodes/properties.

PS C:\> $xml = [xml](get-content file.xml)
PS C:\> $xml

#comment                                                    employees
--------                                                    ---------
 file.xml                                                   employees

PS C:\> $xml.employees

{Frankie Johnny, Elvis Presley, Ella Fitzgerald}

PS C:\> $xml.employees.employee

id                                      name                                    age
--                                      ----                                    ---
101                                     Frankie Johnny                          36
102                                     Elvis Presley                           79
301                                     Ella Fitzgerald                         102

PS C:\> $xml.employees.employee[0].name
Frankie Johnny
PS C:\> $xml.employees.employee[1].age
PS C:\>

The cmdlet get-content is equivalent to cat in UNIX and returns the contents of the file as lines of text.

The square brackets '[xml]' in front of the get-content cmdlet indicate an type object. In this case, it is casting the text returned from get-content file.xml to an XmlDocument object. Once you have an XmlDocument object, PowerShell's builtin support for XML kicks in. The individual XMLElement objects present their children nodes as properties. This means the element name is the property. In this case, the root element is <employees> so it is accessed as a property on the $xml variable. And try this: type '$xml.em' and then hit TAB. That's right, tab completion of the element names. Makes it a little easier for ya.

But Wait, There's More

You still have access to the methods of the underlying XmlElement object too. Execute the command '$xml | gm' to get a list of all the methods and properties available for any XML node. Which means you can use SelectNodes() and SelectSingleNode() using the XPath query syntax. Here's an example using the SelectNodes() method on XmlElement.

PS C:\> $xml = (Get-Content file.xml)
PS C:\> $xml = [xml](Get-Content file.xml)
PS C:\> $xml.SelectNodes("/employees/employee")

id                                      name                                    age
--                                      ----                                    ---
101                                     Frankie Johnny                          36
102                                     Elvis Presley                           79
301                                     Ella Fitzgerald                         102

And by passing those results through the PowerShell pipeline, you can pipe those results into other commands like select-object, where-object and foreach-object to prune the values or execute commands based on the values for a very expressive and powerful scripting experience. Note: I'm using the alias for get-content (gc). Most PowerShell cmdlets have aliases that are an acronym from their fullname.

In the 4 examples below, we use where-object and foreach-object to check properties of the employee nodes so we only return those that match a certain criteria or manipulate them in some way. The first example looks like it should return the 2 employees with age over 50 but it doesn't. The reason is that the properties of the XML objects are always strings. Thus it does a lexicographical comparison. That is easily solved by casting the value to an int using the type cast operator [int] much in the same way we cast the string output of get-content to an xml document [xml].

The 2nd command shows that casting to an int properly returns the employees older than 50. The 3rd example shows that you can call the methods on the string objects returned. So let's say the first digit of your employee id indicates a department, you can easily use the startsWith() method on the String object. What is $_? The '$_' is a variable that represents the current pipeline object. It is used to so that you can manipulate the current object being passed to your function/script block. The where-object cmdlet takes a script block (denoted by the curly brackets { }) and executes the powershell script inside. If the expression evaluates to true, the object is passed down the pipeline to the next command (which in this case is the default output command) and when the where-object script block evaluates to false, the object is "dropped".

If you wanted to manipulate the XML nodes values somehow you would use foreach-object which also takes a script block. And in fact the 4th command uses foreach to create some concatenated string of all the employee nodes values.

PS C:\> $xml = [xml](gc file.xml)
PS C:\> $xml.employees.employee | where { $_.age -gt 50 }

id                                      name                                    age
--                                      ----                                    ---
102                                     Elvis Presley                           79

PS C:\> $xml.employees.employee | where { [int]$_.age -gt 50 }

id                                      name                                    age
--                                      ----                                    ---
102                                     Elvis Presley                           79
301                                     Ella Fitzgerald                         102

PS C:\> $xml.employees.employee | where { $"1") }

id                                      name                                    age
--                                      ----                                    ---
101                                     Frankie Johnny                          36
102                                     Elvis Presley                           79

PS C:\> $xml.employees.employee | foreach { $ + ":" + $ + ":" + $_.age }
101:Frankie Johnny:36
102:Elvis Presley:79
301:Ella Fitzgerald:102

A Really Simple Silly Example

RSS feeds are easy to access and they are basically just XML. So let's see how PowerShell deals with RSS feeds. There are already articles on the InterTube that showcase how easy it is to do this, but I thought I'd include it as well. Grab an RSS feed from a favorite website and see if there are any articles that match some criteria. I'm going to grab TMZ's RSS feed and check for articles with my first name "scott". First I want to show how to grab the XML RSS document and display the articles using 1 or more of their properties. This is done by using the .NET class System.Net.WebClient's DownloadString(String url) method. Very easy to do.

Next I'm going to select articles with the name 'scott' in the title. I will use the -imatch Regex Powershell operator. It does a case insensitive search for the target string anywhere in the input string. Note: Your results may differ as the articles differ by day from TMZ website. Also I'm using 'ft' which is an alias for 'format-table' which lets me select which properties I want to display.

PS C:\powershell> $url = ""
PS C:\powershell> $feed=[xml](new-object$url)
PS C:\powershell> $ | format-table title,link

title                                                       link
-----                                                       ----
Report: Marie Osmond's Son Commits Suicide        
Scotty Lago Conspiracy Theory -- Up in Smoke      
Brittany Murphy -- 109 Mystery Pills              
Liev Schreiber to the Rescue!                     
Scotty Lago's Olympic Conspiracy Theory           
Britney Spears -- The Blonde Is Back              
Former 'Idol' Elliott Yamin in Chile During Quake 
'Pants on the Ground' Guy -- King of Vegas        
Reality to Nas -- 'Memba Me?                      
Nic Cage's Manager -- Get Me Outta This Suit!     
Audrina Patridge:  Bad Acting Got Me Towed        
TMZ's Bangin' Backside Contest -- Bootyfull!      
Conan the Barbarian -- One Hairy Situation        
Carol Brady vs. Mrs. C: Who'd You Rather?         
Nicole Richie & the Chocolate Factory             
Guess Who This Guy Turned Into!                   
Jon Cryer Alleged Hit - Mexico Gang Connection?   
Avril & Deryck's Divorce Takes a Backseat         
World to Joanna Krupa's Mom -- Thank You!         
K-Fed Confused by Green Mystery Substance         

PS C:\powershell> $ | where { $_.title -imatch "scott" } | ft title,link

title                                                       link
-----                                                       ----
Scotty Lago Conspiracy Theory -- Up in Smoke      
Scotty Lago's Olympic Conspiracy Theory           

Small Print and Gotchas

The "item" Property Issue

Powershell automatically adds an "Item" property to each XmlElement. This makes hashtable like access to its properties possible '$feed["rss"]'. Since RSS docs have 'Item' nodes, Powershell complains when it tries to access them. So you must work around that by either changing the name(annoying I know) or writing your code to work past the "item" node as in the examples above. Below is an example of the error you see when trying to access the underlying "Item" nodes.

PS C:\powershell> $
format-default : The member "Item" is already present.
    + CategoryInfo          : NotSpecified: (:) [format-default], ExtendedTypeSystemException
    + FullyQualifiedErrorId : 

Hidden Methods

Some of the underlying methods for XmlDocument and XmlElement are not exposed using get-member. But you can easily get them from the .NET API docs. and any properties defined for the class need to be accessed using their method form (get_Property()). The recommendation is to know the methods on the underlying XmlElement instances or have the API docs handy.

Updating XML

There's no native support for setting values of XmlElements in PowerShell. What you have to do is call the methods of the underlying XmlElement/XmlDocument objects as you would in C#. But remember they don't appear when you do '$xml | gm'. Understanding the methods on the XML .NET classes (XmlElement, XmlDocument) helps here.

Other Resources

Articles describing the "item" property issue better and also some examples of how to modify an XML document in PowerShell and save it back:

Next Steps

At my current job, I'm working with Lucene to build a search index for our music catalog which includes albums, tracks and artists. Lucene is an open-source library for Information Retrieval. It's widely used and has both a Java version and .NET port. We use Java here at work but I've been writing some scripts around Lucene.NET in PowerShell. The Lucene.NET port is several versions behind the Java version unfortunately but still very useful (latest is 2.4). In my next article, I plan on combining the robust XML handling of PowerShell to seamlessly integrate with Lucene.NET so that it's extremely easy to grab XML docs/RSS feeds from the web and index them for fast retrieval.


Hopefully this article has shown how easy it is to work with XML using Windows PowerShell. In addition to the robust XML handling capabilities, I hope to have impressed how expressive one can be joining the pipeline architecture with XML to create very flexible and useful scripts/tools in PowerShell.

This is my first posting to CodeProject. I hope you find it useful and I welcome any and all feedback. Thanks!


  • 27th February, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior) Musicnet
United States United States
Software Engineer at Musicnet / MNDigital. Working on a Java Web Services platform currently but previously worked on C# and C++ projects:
Windows PowerShell - SDE/T 4 years
XNA Game Studio Express - SDE/T 1 year
C++ SIP Stack(dynamicsoft) - SDE for 4 years

I've been working with information retrieval using Lucene for past year.

Comments and Discussions

QuestionXML help Pin
t0m000019-Feb-12 12:19
t0m000019-Feb-12 12:19 
AnswerRe: XML help Pin
shappell21-Feb-12 12:19
shappell21-Feb-12 12:19 
GeneralRe: XML help Pin
t0m000022-Feb-12 3:13
t0m000022-Feb-12 3:13 
GeneralRe: XML help Pin
shappell23-Feb-12 8:13
shappell23-Feb-12 8:13 
GeneralRe: XML help Pin
t0m000011-May-12 2:10
t0m000011-May-12 2:10 
Generalupdating XML [modified] Pin
f5chwiet28-Jan-11 16:46
f5chwiet28-Jan-11 16:46 
Questioncomment/uncomment XML file Pin
Ashrafur Rahaman23-Mar-10 8:18
Ashrafur Rahaman23-Mar-10 8:18 
AnswerRe: comment/uncomment XML file Pin
shappell23-Mar-10 18:40
shappell23-Mar-10 18:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.