Handling XML with PowerShell is very simple. It converts XML elements to properties on .NET objects without the need to write any parsing code. So it is very easy to use XML with PowerShell. Here we are going to discuss how to read the XML file values in different ways.
You can also readย How to add values to the string array from xml using Powershellย andย How to write data to an xml file using Powershellย
A Quick Example
XML File :ย MyXM.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" encoding="ISO-8859-1"?> <EmpDetails> <Person> <Id>E00023</Id> <mailId>Aadharsh@dotnet-helper.com</mailId> <empName>Aadharsh G.K</empName> </Person> <Person> <Id>E00042</Id> <mailId>Rakshu@dotnet-helper.com</mailId> <empName>RakshaNivasini S.T</empName> </Person> </EmpDetails> |
Method 1:
Once you’ve run Get-Content to read the raw text from the XML document and cast the output to type [XML], you now have a variable called $XmlDocumentย that contains the entire XML node tree which we can process.
STEPย #1: Assign the XML file location into the variable.
$XMLfile = ‘C:\donet-helpers\Demo\MyXM.xml’
STEPย #2: Read the xml file using get-content cmdlet and assign to variable. The [xml] casts the variable as an XML object.
[XML]$empDetails = Get-Content $XMLfile
STEPย #3: Looping the child node to get the value.
foreach($empDetail in $empDetails.EmpDetails.Person){ }
Final Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
############################################################ #Project : Reading XML files with PowerShell #Developer : Thiyagu S (dotnet-helpers.com) #Tools : PowerShell 5.1.15063.1155 #E-Mail : mail2thiyaguji@gmail.com ############################################################<br /> $XMLfile = 'C:\D_EMS Drive\Personal\LBLOG\testpath.xml' [XML]$empDetails = Get-Content $XMLfile foreach($empDetail in $empDetails.EmpDetails.Person){ Write-Host "Employee Id :" $empDetail.Id Write-Host "Employee mail Id :" $empDetail.mailId Write-Host "Employee Name :" $empDetail.empName Write-Host '' } |
OUTPUT:
Method 2:
STEPย #1: Assign the XML file location into the variable.
$Path = “C:\donet-helpers\Demo\MyXM.xml”
STEPย #2: The below command saves the XML path to the AliasProperty node in the $XPath variable.
$XPath = “/EmpDetails/Person”
STEPย #3: The Select-Xml cmdlet lets you use XPath queries to search for text in XML strings and documents. Enter an XPath query, and use the Content, Path, or Xml parameter to specify the XML to be searched.
Select-Xml -Path $Path -XPath $Xpath | Select-Object -ExpandProperty Node
Final Code:
1 2 3 4 5 6 7 8 9 |
############################################################ #Project : Reading XML files with PowerShell #Developer : Thiyagu S (dotnet-helpers.com) #Tools : PowerShell 5.1.15063.1155 #E-Mail : mail2thiyaguji@gmail.com ############################################################<br /> $Path = "C:\D_EMS Drive\Personal\LBLOG\testpath.xml" $XPath = "/EmpDetails/Person" Select-Xml -Path $Path -XPath $Xpath | Select-Object -ExpandProperty Node |
What do you think?
I hope you have an idea of how to Reading XML Files With PowerShell. I would like to have feedback from the readers of my posts. Your valuable feedback, question, or comments about this article are always welcome.
Typo:
its “http://192.168.1.100:11111/v3/state”
Hi,
How must the query look like, if the XML-FIle is not located locally in a directory, but is retrieved via the API of a web server?
example: http://:11111/v3/state
For reading XML documents with correct encoding, you should always use:
$xml = New-Object xml
$xml.Load( filename )
The Get-Content method only works by luck, because most XML documents are UTF-8 encoded, which happens to be the default encoding used by Get-Content which has no knowledge of the XML “encoding” attribute.
Using Get-Content to read the file is dangerous since it requires the xml file read to be in a specific encoding. The XML format has encoding build in but that value is ignored when first reading the file as a string.