Thursday, December 4, 2014

Reading XML with PowerShell

Here is an example of accessing XML data via PowerShell:

Script:
$xmlDocument = [xml] @"
<catalog>
   <book id="101">
      <author>Author1</author>
      <title>Title1</title>
      <genre>Genere1</genre>
      <price>45.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>This is a description1.</description>
   </book>
   <book id="101">
      <author>Author2</author>
      <title>Title2</title>
      <genre>Genere2</genre>
      <price>75.99</price>
      <publish_date>1998-7-15</publish_date>
      <description>This is a description2.</description>
   </book>
</catalog>
"@

$xmlDocument.catalog.book | Select-Object -Property author, title, price

Result:
PS C:\Users\tnaveed\Desktop> C:\Users\tnaveed\Desktop\CheckUser.ps1

author                         title                         price                        
------                         -----                         -----                        
Author1                        Title1                        45.95                        
Author2                        Title2                        75.99