How to read an XML file in PowerShell 2010


In this article we will be seeing how to read an xml file in powershell 2010.

In this we will be creating a simple xml file and we will be reading the xml file in the powershell.

Test.xml

<? Xml version="1.0" encoding="utf-8" ?>
<customers>
  <
Name>Anand</Name>
  <Id>123</Id>
  <Age>23</Age>
  <Location>Bangalore</Location>
</customers>

Get the content from the xml file:

[Xml]$xmldata=Get-Content C:\Users\kpast\Desktop\Test.xml

1.gif

Get all the nodes:

$xmldata.customers

2.gif

Get a particular node:

$xmldata.customers.Name

3.gif

Assign the xml value to a variable:

$variable=$xmldata.customers.Name

Result:

$variable will be equal to Anand.
  

Up Next
    Ebook Download
    View all
    Learn
    View all