Reading XML

Hi all,

I’ve recently started using PowerShell to make my life easier in my job.

I often have to check Web.Config files on different servers and want to do this using PS as there can be 10 different App’s running on the same server all with their own Web.Config. Rather then just search for a string I would like to use the actual key and print out the value.

The XML files have multiple levels of nested elements, and after googling there seems to be so many different opinions on how to do this.

Can someone help point me in the right direction, maybe an e-book or video that covers how to get started.





There’s isn’t a cmdlet to do it, but with a cast:

PS /Users/js> [xml]$xml = get-content test.xml                                   
PS /Users/js> $xml                                                               


PS /Users/js> $xml.objs                                                          

Version xmlns                                           Obj
------- -----                                           --- {Obj, Obj, Obj, Obj}

# if you want to save changes
PS /Users/js> $'test.xml')

Thanks for the reply, when I try your code it just opens the XML in IE.

Would I then be able to do something like

Write-Host $xml.object.value 

It should work. What are you running? It sounds like you’re just running ‘test.xml’.

No matter what you read, you are going to have to decide what works for you. Meaning, what you can grasp, understand and internalize for future needs.

There are lots of articles all over the web regarding using PS to create, read, update and delete XML files and or their content. As you’ve obviously already found, but don’t let that overwhelm you. Be specific in the task you are after, and attack it one step at a time. PS is natively XML heavy in many respects.

There are cmdlets specifically for dealing XML as well as leveraging the .Net namespace for it.

Get-Command -Name '*xml*'

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          ConvertTo-Xml                                Microsoft.PowerShell.Utility
Cmdlet          Convert-XMLtoJSON                            Sorlov.PowerShell
Cmdlet          Export-Clixml                                Microsoft.PowerShell.Utility
Cmdlet          Import-Clixml                                Microsoft.PowerShell.Utility
Cmdlet          Merge-XMLFile                                Sorlov.PowerShell
Cmdlet          New-XSDfromXML                               Sorlov.PowerShell
Cmdlet          Select-Xml                                   Microsoft.PowerShell.Utility

Of course using any of what is hear requires you know what you are after and use the correct resource.

Here are few examples I’ve passed on.

PowerShell Data Basics: XML - Simple Talk

•Accessing XML data in PowerShell ◦Accessing XML with XPath.
◦Accessing XML as Objects.
◦Comparison of XPath and Object Approaches

•Modifying or Creating XML Data ◦Adding XML Data
◦Using XML for Object Serialization

Mastering everyday XML tasks in PowerShell
The Scripting Wife Learns to Use PowerShell to Work with XML

As for videos, YouTube is there for that as well…

There are also free and fee-oriented modules you can acquire:

PowerShell Gallery | Power-XML

PowerShell Gallery | Xml 7.0

XML PowerShell Cmdlets An easy-to-use set of PowerShell Cmdlets offering real-time access to XML data.

I don’t know of any cmdlet that would work like an “import-xml”. Import-clixml won’t work with a typical xml file.

You were correct, I totally forgot the Get-Content.



Thank you very much for the links, I’ve not had a chance to go through all of them yet but that ‘Scripting Wife’ blog was a great starting point and exactly what I needed to get me going.

Following through the blog I’ve hit a bit of a snag. If i’m understanding this correctly from the online stuff I’ve read, the end result is being put into a hash table…I think, I’m looking for the value of DataPortalServer, so when I do $xml.configuration.appsettings.add.value[6] this gives me the information that I’m looking for. However, the index may not always be the same, so I want to address it by the name of the key.

I’ve tried .ContainsKey(‘DataPortalServer’), I’ve tried $xml.configuration.appsettings.add.value[‘DataPortalServer’] as well as several other things that I’ve googled but I just can’t get it to work.

Thank’s so much for what you’ve already provided.


Function QPConfigFiles{
[xml]$xml=get-content C:\test.xml
$xml.configuration.appsettings.add ('CslaDataPortalUrl')
<!-- Dataportal -->
<add key="SystemUser" value="" />
<add key="SystemPW" value="" />
<add key="Authentication" value="CSLA" />
<add key="Location" value="Client" />
<add key="ChangeMonitorIntervalMins" value="2" />
<add key="CslaDataPortalProxy" value="Csla.DataPortalClient.GenuineChannelsProxy, Csla3" />
<add key="CslaDataPortalUrl" value="ghttp://localhost:8753/Rawr/RemotingPortal.rem" />
<add key="DataPortalServer" value="ghttp://localhost:8753/Rawr/DataPortal.rem" />



Hi, I don’t see your latest reply. Unfortunately this forum can’t handle xml. You can make a gist of the xml and link to it here. (Maybe this forum should use the technet system?) Actually select-xml has some potential. But I don’t know xpath. Xpath runs deep. Xpath filters can be used with get-winevent as well.

xpath info:

I was trying this. Both $a.node and $xml.objs are XmlElement types.

# Selects the first book element that is the child 
#  of the bookstore element
$a = select-xml /bookstore/book[1] xpath.xml

# Selects all the title elements (anywhere) that have a "lang" attribute 
#  with a value of "en"
(select-xml "//title[@lang='en']" xpath.xml).node

lang #text
---- -----
en   Everyday Italian
en   Harry Potter
en   XQuery Kick Start
en   Learning XML

[xml]$xml = get-content xpath.xml

# same thing
$xml = (select-xml / xpath.xml).node   

It’s a shame that I can’t see my own post to edit it :frowning:

I’ve linked the gist above.

The links posted have been great, especially the scripting wife blog. From what I can see, im at a stage where it’s put the detail into a hast table.


This line works, but I want to address it by a name not by index number, $xml.configuration.appsettings.add.value[‘DataPortalServer’]


Something like that, but I can’t for the life of me get it to work.


It’s not a hash table. You can do it this way:

$xml.configuration.appsettings.add | where key -eq DataPortalServer | 
  select -expand value 


Or using xpath, which is case sensitive. With select-xml, the node property has the resulting object.

(Select-Xml "/configuration/appSettings/add[@key='DataPortalServer']" example.xml).node.value  


That’s great. I’ve almost got what I’m looking for!

Function QPConfigFilesxpath{
cfgpath = 'C:\inetpub\Qp\Web.Config'
param1 = 'ApiURL'
cfgpath ='C:\inetpub\IdentityServer\Web.Config'

for($i=0; $i -lt $configfiles.Length; $i++)
#write-output $configfiles[$i].param2
write-host `n

#[xml]$xml=get-content $configfiles[0].cfgpath

#Write-Output $b

This issue seems to be with [pre]$a=$xml.selectNodes("//add[@key=`"$configfiles[$i].param2`"]")[pre]

The write-out line above it prints the right then when I uncomment it but it doesnt work on the selectnodes.



That’s a difficult quoting situation. How about:

$key = $configfiles[$i].param2
# selectnodes() takes an xpath string
# '//add' means search for an add node anywhere
$a = $xml.selectNodes("//add[@key='$key']")

But I don’t see an ‘ApiUrl’ key in your xml. I guess it’s in a different xml. Also the bottom closing ‘< /configuration >’ tag is missing in the xml.

Unless you want to use the “$( )” method, but it gets more complex and less readable. “$object[$index]” or “$” doesn’t work inside a string the way you would want.

$a = $xml.selectNodes("//add[@key=`"$($configfiles[$i].param2)`"]")