{"componentChunkName":"component---src-templates-articles-index-jsx","path":"/articles/powershell-read-xml-files/","result":{"data":{"props":{"adsense":true,"adsenseMobile":null,"byLine":"","canonical":"https://staging.business.com/articles/powershell-read-xml-files/","content":"\n<p>PowerShell offers several ways to read XML documents, without writing a lot of code or using XPath. Here's how you can get started.</p>\r\n<p><picture class=\"aligncenter full\">Manipulating XML is one of those topics that makes anyone that's ever done it cringe. It's typically time-consuming, hard to understand and just a general pain in the butt. Messing with XML on some other languages besides PowerShell requires essentially memorizing the XML tree to make sure you're manipulating the right node at the right level. On top of that, you'll probably also have to learn to write obscure </picture><a href=\"http://www.w3schools.com/xpath/\" target=\"_blank\">XPath</a><picture class=\"aligncenter full\"> queries to find the content in a XML document.</picture><br><picture class=\"aligncenter full\"><source srcset=\"/images/content/5b6/a458f51ee4793468b4581/1200_-900- 1200w, /images/content/5b6/a458f51ee4793468b4581/668_-501- 668w, /images/content/5b6/a458f51ee4793468b4581/320_-240- 320w\" sizes=\"668px, 100%\"></source></picture></p>\r\n<div id=\"in-article-vca-1\"> </div>\r\n<h2>PowerShell and Reading XML files</h2>\r\n<p>PowerShell still has XPath but you don't necessarily have to use it. PowerShell provides an easy way to read XML files, manipulate them and finally to save them back to disk without writing a lot of code or knowing XPath. PowerShell does this by providing the user dot notation to signify each node in the XML document. But first, let's get started reading an XML document. </p>\r\n<p>One way to read an XML document in PowerShell is to <a href=\"http://en.wikipedia.org/wiki/Type_conversion\" target=\"_blank\">typecast</a> a variable to the type <strong>[xml]</strong>. To create this variable, we can use the <strong>Get-Content</strong> cmdlet to read all of the text in an XML document. To typecast the output of Get-Content we can simply prepend the text <strong>[xml]</strong> before the variable. This tells PowerShell that we need this variable typecasted as a <strong>System.Xml.XmlDocument</strong> type instead of the default array type that normally comes from Get-Content.</p>\r\n<blockquote>\r\n<p>    [xml]$XmlDocument = Get-Content -Path C:Cars.xml</p>\r\n</blockquote>\r\n<p>In this article, we'll be using an XML file called Cars.xml as an example, which contains various makes and models of cars.</p>\r\n<p><picture class=\"aligncenter med\"><source srcset=\"/images/content/5b6/a45c392a3a414438b4586/742_-523- 742w, /images/content/5b6/a45c392a3a414438b4586/412_-290- 412w, /images/content/5b6/a45c392a3a414438b4586/206_-145- 206w\" sizes=\"(min-width: 870px) 412px, (min-width: 400px) 38vw, 62%\"></source><img src=\"/images/content/5b6/a45c392a3a414438b4586/366-258-\" alt=\"\" width=\"366\" height=\"258\" data-content-img-id=\"5b6a45c392a3a414438b4586\"></picture></p>\r\n<p>Once you've run Get-Content to read the raw text from the XML document and cast the output to type System.Xml.XmlDocument, you now have a variable called <strong>$XmlDocument</strong> that contains the entire XML node tree that represents that document.</p>\r\n<p><picture class=\"aligncenter med\"><source srcset=\"/images/content/5b6/a45e251ee47a1488b4580/742_-208- 742w, /images/content/5b6/a45e251ee47a1488b4580/412_-115- 412w, /images/content/5b6/a45e251ee47a1488b4580/206_-57- 206w\" sizes=\"(min-width: 870px) 412px, (min-width: 400px) 38vw, 62%\"></source><img src=\"/images/content/5b6/a45e251ee47a1488b4580/360-101-\" alt=\"\" width=\"360\" height=\"101\" data-content-img-id=\"5b6a45e251ee47a1488b4580\"></picture></p>\r\n<p>At this point, reading the document is a trivial task due to the aforementioned dot notation. Since we now have the XML document in memory we can explore the tree.</p>\r\n<p><picture class=\"aligncenter med\"><source srcset=\"/images/content/5b6/a45f71039ebca718b45e3/742_-473- 742w, /images/content/5b6/a45f71039ebca718b45e3/412_-263- 412w, /images/content/5b6/a45f71039ebca718b45e3/206_-131- 206w\" sizes=\"(min-width: 870px) 412px, (min-width: 400px) 38vw, 62%\"></source><img src=\"/images/content/5b6/a45f71039ebca718b45e3/404-258-\" alt=\"\" width=\"404\" height=\"258\" data-content-img-id=\"5b6a45f71039ebca718b45e3\"></picture></p>\r\n<p>Notice how we can get deeper and deeper into the document using dot notation? If we hit a spot in the document that contains node of like type (Cars) we can then treat it like a PowerShell array and specify an index number of the node we'm looking for. In this case, we're finding the first car in the document by specifying the index number of 0.</p>\r\n<p>Let's say you want to find all of the cars in this XML document that have four or more seats. Simply by using the common PowerShell cmdlet <strong>Where-Object</strong> we can easily retrieve that information.</p>\r\n<h2>Combining Nodes</h2>\r\n<p>Notice that PowerShell combines both the nodes (elements) and XML attributes all in one set? This allows you to easily comb through the document just by using dot notation and the Where-Object cmdlet. PowerShell knows all the attributes and sub elements that are associated with each parent XML and displays them in a nice table for you, by default.</p>\r\n<p><picture class=\"aligncenter med\"><source srcset=\"/images/content/5b6/a461392a3a4323c8b4586/742_-522- 742w, /images/content/5b6/a461392a3a4323c8b4586/412_-290- 412w, /images/content/5b6/a461392a3a4323c8b4586/206_-145- 206w\" sizes=\"(min-width: 870px) 412px, (min-width: 400px) 38vw, 62%\"></source><img src=\"/images/content/5b6/a461392a3a4323c8b4586/362-255-\" alt=\"\" width=\"362\" height=\"255\" data-content-img-id=\"5b6a461392a3a4323c8b4586\"></picture></p>\r\n<p>We've shown you one method that you can use to read XML documents with. One of the nice things about PowerShell is it can be as easy or as hard as you want it to be. We've shown you how to find nodes in a XML document using dot notation. If you ever have the need to do more advanced searching, we  suggest you look into using cmdlets like <strong><a href=\"https://technet.microsoft.com/en-us/library/hh849968.aspx?f=255&amp;MSPPError=-2147217396\" target=\"_blank\">Select-Xml</a></strong> to build more powerful queries. Either way, PowerShell will allow you to read and find just about anything from an XML document. The rest is up to you.</p>\r\n<div id=\"in-article-vca-2\"> </div>\n","docType":"article","guid":"articles_06bd5b3b932917255cf79eb6c8bd6ed2","lastModified":"2018-09-05T12:01:00.000Z","published":"2018-09-05T12:00:00.000Z","structuredData":null,"subtitle":"PowerShell offers several ways to read XML documents, without writing a lot of code.","template":"v1","title":"Introduction To Using XML Files With PowerShell","type":"article","author":{"avatar":null,"bio":"Adam Bertram is a 20-year veteran of IT and experienced online business professional. He's an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn, or follow him on Twitter at @adbertram or the TechSnips Twitter account at @techsnips_io.","name":"Adam Bertram","image":null,"slug":null,"social":{"facebook":"","linkedin":"https://www.linkedin.com/in/adambertram/","twitter":"https://twitter.com/adbertram","website":"https://www.adamtheautomator.com/"},"status":"enabled","title":null,"type":"Contributing Writer","user":{"imagePath":"https://staging.business.com/images/undefined","profile":{"company":"eGain Communications","jobTitle":"Product Management Analyst","website":""}}},"breadcrumbs":null,"channels":{"primary":{"name":"Technology","slug":"technology"},"sub":{"name":"Technology Solutions","slug":"technology-solutions"}},"meta":{"robots":"index, follow","description":"PowerShell offers a number of ways to read XML documents, without having to write a lot of code or using XPath. Here's how you can get started.","title":"How to read xml files with PowerShell - business.com"},"parsedContent":{"toc":[{"id":"powershell-and-reading-xml-files","title":"PowerShell and Reading XML files"},{"id":"combining-nodes","title":"Combining Nodes"}],"json":[{"component":"p","children":["PowerShell offers several ways to read XML documents, without writing a lot of code or using XPath. Here's how you can get started."]},{"component":"p","children":[{"component":"picture","props":{"className":"aligncenter full"},"children":["Manipulating XML is one of those topics that makes anyone that's ever done it cringe. It's typically time-consuming, hard to understand and just a general pain in the butt. Messing with XML on some other languages besides PowerShell requires essentially memorizing the XML tree to make sure you're manipulating the right node at the right level. On top of that, you'll probably also have to learn to write obscure "]},{"component":"a","props":{"href":"http://www.w3schools.com/xpath/","target":"_blank"},"children":["XPath"]},{"component":"picture","props":{"className":"aligncenter full"},"children":[" queries to find the content in a XML document."]},{"component":"br"},{"component":"picture","props":{"className":"aligncenter full"},"children":[{"component":"source","props":{"srcSet":"/images/content/5b6/a458f51ee4793468b4581/1200_-900- 1200w, /images/content/5b6/a458f51ee4793468b4581/668_-501- 668w, /images/content/5b6/a458f51ee4793468b4581/320_-240- 320w","sizes":"668px, 100%"}}]}]},{"component":"div","props":{"id":"in-article-vca-1"},"children":[" "]},{"component":"h2","props":{"id":"powershell-and-reading-xml-files"},"children":["PowerShell and Reading XML files"]},{"component":"p","children":["PowerShell still has XPath but you don't necessarily have to use it. PowerShell provides an easy way to read XML files, manipulate them and finally to save them back to disk without writing a lot of code or knowing XPath. PowerShell does this by providing the user dot notation to signify each node in the XML document. But first, let's get started reading an XML document. "]},{"component":"p","children":["One way to read an XML document in PowerShell is to ",{"component":"a","props":{"href":"http://en.wikipedia.org/wiki/Type_conversion","target":"_blank"},"children":["typecast"]}," a variable to the type ",{"component":"strong","children":["[xml]"]},". To create this variable, we can use the ",{"component":"strong","children":["Get-Content"]}," cmdlet to read all of the text in an XML document. To typecast the output of Get-Content we can simply prepend the text ",{"component":"strong","children":["[xml]"]}," before the variable. This tells PowerShell that we need this variable typecasted as a ",{"component":"strong","children":["System.Xml.XmlDocument"]}," type instead of the default array type that normally comes from Get-Content."]},{"component":"blockquote","children":[{"component":"p","children":["    [xml]$XmlDocument = Get-Content -Path C:Cars.xml"]}]},{"component":"p","children":["In this article, we'll be using an XML file called Cars.xml as an example, which contains various makes and models of cars."]},{"component":"p","children":[{"component":"picture","props":{"className":"aligncenter med"},"children":[{"component":"source","props":{"srcSet":"/images/content/5b6/a45c392a3a414438b4586/742_-523- 742w, /images/content/5b6/a45c392a3a414438b4586/412_-290- 412w, /images/content/5b6/a45c392a3a414438b4586/206_-145- 206w","sizes":"(min-width: 870px) 412px, (min-width: 400px) 38vw, 62%"}},{"component":"img","props":{"src":"/images/content/5b6/a45c392a3a414438b4586/366-258-","alt":"","width":"366","height":"258","data-content-img-id":"5b6a45c392a3a414438b4586"}}]}]},{"component":"p","children":["Once you've run Get-Content to read the raw text from the XML document and cast the output to type System.Xml.XmlDocument, you now have a variable called ",{"component":"strong","children":["$XmlDocument"]}," that contains the entire XML node tree that represents that document."]},{"component":"p","children":[{"component":"picture","props":{"className":"aligncenter med"},"children":[{"component":"source","props":{"srcSet":"/images/content/5b6/a45e251ee47a1488b4580/742_-208- 742w, /images/content/5b6/a45e251ee47a1488b4580/412_-115- 412w, /images/content/5b6/a45e251ee47a1488b4580/206_-57- 206w","sizes":"(min-width: 870px) 412px, (min-width: 400px) 38vw, 62%"}},{"component":"img","props":{"src":"/images/content/5b6/a45e251ee47a1488b4580/360-101-","alt":"","width":"360","height":"101","data-content-img-id":"5b6a45e251ee47a1488b4580"}}]}]},{"component":"p","children":["At this point, reading the document is a trivial task due to the aforementioned dot notation. Since we now have the XML document in memory we can explore the tree."]},{"component":"p","children":[{"component":"picture","props":{"className":"aligncenter med"},"children":[{"component":"source","props":{"srcSet":"/images/content/5b6/a45f71039ebca718b45e3/742_-473- 742w, /images/content/5b6/a45f71039ebca718b45e3/412_-263- 412w, /images/content/5b6/a45f71039ebca718b45e3/206_-131- 206w","sizes":"(min-width: 870px) 412px, (min-width: 400px) 38vw, 62%"}},{"component":"img","props":{"src":"/images/content/5b6/a45f71039ebca718b45e3/404-258-","alt":"","width":"404","height":"258","data-content-img-id":"5b6a45f71039ebca718b45e3"}}]}]},{"component":"p","children":["Notice how we can get deeper and deeper into the document using dot notation? If we hit a spot in the document that contains node of like type (Cars) we can then treat it like a PowerShell array and specify an index number of the node we'm looking for. In this case, we're finding the first car in the document by specifying the index number of 0."]},{"component":"p","children":["Let's say you want to find all of the cars in this XML document that have four or more seats. Simply by using the common PowerShell cmdlet ",{"component":"strong","children":["Where-Object"]}," we can easily retrieve that information."]},{"component":"h2","props":{"id":"combining-nodes"},"children":["Combining Nodes"]},{"component":"p","children":["Notice that PowerShell combines both the nodes (elements) and XML attributes all in one set? This allows you to easily comb through the document just by using dot notation and the Where-Object cmdlet. PowerShell knows all the attributes and sub elements that are associated with each parent XML and displays them in a nice table for you, by default."]},{"component":"p","children":[{"component":"picture","props":{"className":"aligncenter med"},"children":[{"component":"source","props":{"srcSet":"/images/content/5b6/a461392a3a4323c8b4586/742_-522- 742w, /images/content/5b6/a461392a3a4323c8b4586/412_-290- 412w, /images/content/5b6/a461392a3a4323c8b4586/206_-145- 206w","sizes":"(min-width: 870px) 412px, (min-width: 400px) 38vw, 62%"}},{"component":"img","props":{"src":"/images/content/5b6/a461392a3a4323c8b4586/362-255-","alt":"","width":"362","height":"255","data-content-img-id":"5b6a461392a3a4323c8b4586"}}]}]},{"component":"p","children":["We've shown you one method that you can use to read XML documents with. One of the nice things about PowerShell is it can be as easy or as hard as you want it to be. We've shown you how to find nodes in a XML document using dot notation. If you ever have the need to do more advanced searching, we  suggest you look into using cmdlets like ",{"component":"strong","children":[{"component":"a","props":{"href":"https://technet.microsoft.com/en-us/library/hh849968.aspx?f=255&MSPPError=-2147217396","target":"_blank"},"children":["Select-Xml"]}]}," to build more powerful queries. Either way, PowerShell will allow you to read and find just about anything from an XML document. The rest is up to you."]},{"component":"div","props":{"id":"in-article-vca-2"},"children":[" "]}]},"resource":null,"sponsorship":null,"thumbnail":{"path":"https://staging.business.com/images/content/5db/f21da9c658a05188b4577/1500-0-","caption":"scyther5/Getty","isBlurred":false},"bestPicks":null},"related":{"relatedArticles":[{"title":"Introduction To Using XML Files With PowerShell","canonical":"https://www.business.com/articles/powershell-read-xml-files/","slug":"powershell-read-xml-files","thumbnail":"https://www.business.com/images/content/5db/f21da9c658a05188b4577/1500-0-","dek":"PowerShell offers several ways to read XML documents, without writing a lot of code.","lastModified":"2018-09-05T12:01:00Z","published":"2018-09-05T12:00:00Z","sponsored":false},{"title":"PowerShell: Everything You Need to Know","canonical":"https://www.business.com/articles/powershell-everything-you-need-to-know/","slug":"powershell-everything-you-need-to-know","thumbnail":"https://www.business.com/images/content/5b8/998581039eb8f738b457b/1500-0-","dek":"What you need to know to unlock Microsoft PowerShell's full capabilities.","lastModified":"2018-09-05T17:00:00Z","published":"2018-09-05T17:00:00Z","sponsored":false},{"title":"Essential PowerShell Cmdlets For Managing Active Directory","canonical":"https://www.business.com/articles/powershell-active-directory-cmdlets/","slug":"powershell-active-directory-cmdlets","thumbnail":"https://www.business.com/images/content/5cf/90cab7b4374ab068b456f/1500-0-","dek":"Administering Active Directory is one of the most critical roles in any Windows network.","lastModified":"2018-09-04T08:00:00Z","published":"2018-09-04T08:00:00Z","sponsored":false},{"title":"Essential PowerShell Cmdlets For Auditing And Maintaining Storage","canonical":"https://www.business.com/articles/powershell-for-storage/","slug":"powershell-for-storage","thumbnail":"https://www.business.com/images/content/5d0/18e065a215ea2498b4588/1500-0-","dek":"PowerShell v5 offers some new features for managing storage.","lastModified":"2018-09-04T11:00:00Z","published":"2018-09-04T11:00:00Z","sponsored":false},{"title":"How and When to Create and Use PowerShell Modules","canonical":"https://www.business.com/articles/powershell-modules/","slug":"powershell-modules","thumbnail":"https://www.business.com/images/content/5cf/a71415a215e501f8b457a/1500-0-","dek":"Step-by-step instructions on creating and using modules.","lastModified":"2018-09-05T08:00:00Z","published":"2018-09-05T08:00:00Z","sponsored":false},{"title":"How to Hire for Your Business","canonical":"https://www.business.com/articles/how-to-staff-business/","slug":"how-to-staff-business","thumbnail":"https://www.business.com/images/content/5ea/9c8bb9c658aee018b456c/1500-0-","dek":"Sterling Staffing Solutions CEO Stephen Carter shares his tips on staffing your business.","lastModified":"2020-07-31T15:00:00Z","published":"2018-08-16T12:00:00Z","sponsored":false},{"title":"Your Guide to Creating a Small Business Marketing Plan","canonical":"https://www.business.com/articles/sample-marketing-plan-outline-and-template/","slug":"sample-marketing-plan-outline-and-template","thumbnail":"https://www.business.com/images/content/5f3/3e1765a215eb62d8b4567/1500-0-","dek":"Follow these templates and guidelines to get started on creating your business's marketing plan.","lastModified":"2020-08-12T12:28:00Z","published":"2018-03-02T13:00:00Z","sponsored":false},{"title":"Introduction To Using Text Files With PowerShell","canonical":"https://www.business.com/articles/powershell-read-text-files/","slug":"powershell-read-text-files","thumbnail":"https://www.business.com/images/content/5db/f21fe5a215edb4f8b4571/1500-0-","dek":"Learn the basics: how to retrieve text from text files and how to find text in text files.","lastModified":"2018-09-05T10:00:00Z","published":"2018-09-05T10:00:00Z","sponsored":false},{"title":"How to Make Money in Retirement: A Guide to Turning a Hobby into a Side Business","canonical":"https://www.business.com/articles/how-make-money-retirement-hobby/","slug":"how-make-money-retirement-hobby","thumbnail":"https://www.business.com/images/content/5ba/902e01039eb26648b4572/1500-0-","dek":"Many retirees turn side interests into income. Learn how to make money in retirement.","lastModified":"2018-09-24T15:03:00Z","published":"2018-09-24T15:03:00Z","sponsored":false},{"title":"Using Lean Strategies to Achieve Team Synergy and Efficiency","canonical":"https://www.business.com/articles/lean-strategies-team-efficiency/","slug":"lean-strategies-team-efficiency","thumbnail":"https://www.business.com/images/content/5be/480e49c658a51108b458c/1500-0-","dek":"Learn more about lean manufacturing strategies and how to implement them in your business.","lastModified":"2018-11-08T08:00:00Z","published":"2018-11-08T08:00:00Z","sponsored":false},{"title":"From COVID-19 to Hurricane Season: Disaster Preparedness for Small Business","canonical":"https://www.business.com/articles/business-disaster-prep/","slug":"business-disaster-prep","thumbnail":"https://www.business.com/images/content/5e7/a495e5a215eb11d8b4569/1500-0-","dek":"Natural disasters – whether a hurricane, tornado or outbreak like COVID-19 – can have a disastrous effect on small businesses, but they don't have to. Here's how to weather a disaster successfully.","lastModified":"2020-03-25T15:19:00Z","published":"2018-09-26T07:00:00Z","sponsored":false},{"title":"Have a WordPress Site? Optimize It.","canonical":"https://www.business.com/articles/have-a-wordpress-site-optimize-it/","slug":"have-a-wordpress-site-optimize-it","thumbnail":"https://www.business.com/images/content/5db/f1c7b5a215ed94f8b4574/1500-0-","dek":"Here's how to get the most out of your WordPress site and compete with large retailers.","lastModified":"2018-10-24T07:00:00Z","published":"2018-10-24T07:00:00Z","sponsored":false}],"relatedReviews":[{"doc_type":"category","thumbnail":"https://staging.business.com/images/rz/5df908cc5a215e8c4a8b4568/conferencecall_Antonio_Diaz_Getty.jpg","canonical":"https://staging.business.com/categories/best-conference-call-services/","title":"The Best Conference Call Services of 2021","dek":"Which conference call service is right for your business in 2021? We review the top-rated services.","slug":"best-conference-call-services","lastModified":"2021-03-17T18:17:00Z"},{"doc_type":"category","thumbnail":"https://staging.business.com/images/rz/5df3d8695a215e58428b4570/POS_GETTY_DERO2084.jpg","canonical":"https://staging.business.com/categories/best-pos-systems/","title":"The Best POS Systems of 2021","dek":"Before you choose a POS system, check out our reviews of the best picks for 2021. ","slug":"best-pos-systems","lastModified":"2021-03-17T17:33:00Z"},{"doc_type":"category","thumbnail":"https://staging.business.com/images/rz/5e00d4787b43745d6a8b456e/Laptop_undrey_getty.jpg","canonical":"https://staging.business.com/categories/employee-monitoring-software/","title":"The Best Employee Monitoring Software of 2021","dek":"We've narrowed down the best employee monitoring software for businesses. Check out our best picks and reviews.","slug":"employee-monitoring-software","lastModified":"2021-03-05T15:40:00Z"}],"relatedSponsoredArticles":null}},"pageContext":{"displayModified":"2018-09-05T12:01:00.000Z","template":"v1","title":"Introduction To Using XML Files With PowerShell","meta":{"robots":"index, follow"},"docType":"articles","slug":"powershell-read-xml-files","canonical":"https://staging.business.com/articles/powershell-read-xml-files/","skipTOC":false}},"staticQueryHashes":[]}