{"id":207,"date":"2009-09-22T22:07:19","date_gmt":"2009-09-23T03:07:19","guid":{"rendered":"http:\/\/nukeitmike.com\/blog\/2009\/09\/22\/using-xml-in-your-powershell-scripting\/%20"},"modified":"2009-09-22T22:07:19","modified_gmt":"2009-09-23T03:07:19","slug":"using-xml-in-your-powershell-scripting","status":"publish","type":"post","link":"https:\/\/blog.nukeitmike.com\/index.php\/2009\/09\/22\/using-xml-in-your-powershell-scripting\/","title":{"rendered":"Using XML in your PowerShell scripting"},"content":{"rendered":"<p>I have written a lot of scripts that use .txt files to read or store data, but I have a need to read some information from an .xml file.\u00a0 This could be done by treating the file as a simple txt file, but it would require some pretty good filtering that is already a part of the xml file.\u00a0 A quick search helped me locate this article:<\/p>\n<blockquote><p><a title=\"http:\/\/blogs.msdn.com\/kalleb\/archive\/2008\/07\/19\/using-powershell-to-read-xml-files.aspx\" href=\"http:\/\/blogs.msdn.com\/kalleb\/archive\/2008\/07\/19\/using-powershell-to-read-xml-files.aspx\">http:\/\/blogs.msdn.com\/kalleb\/archive\/2008\/07\/19\/using-powershell-to-read-xml-files.aspx<\/a><\/p><\/blockquote>\n<p>Which contained the key to helping me with what I needed to do.\u00a0 The specific piece I needed was in Lesson 2:<\/p>\n<blockquote><p>Lesson 2:<br \/>\nRead data from an XML-file.<br \/>\nThe XML-file that I&#8217;m going to read from has the following structure:<br \/>\n&lt;Users&gt;<br \/>\n\u00a0 &lt;User&gt;<br \/>\n\u00a0\u00a0\u00a0 &lt;Name&gt;Kalle&lt;\/Name&gt;<br \/>\n&lt;\/User&gt;<br \/>\n\u00a0 &lt;User&gt;<br \/>\n\u00a0\u00a0\u00a0 &lt;Name&gt;Becker&lt;\/Name&gt;<br \/>\n\u00a0 &lt;\/User&gt;<br \/>\n&lt;\/Users&gt;<br \/>\nReading data from an XML-file is really easy in PowerShell! Use this command to load the file into an variable:<br \/>\nPS C:\\Tmp&gt; <em>[xml]$userfile = Get-Content Accounts.xml <\/em><\/p>\n<p>When the xml-file is loaded you can type <em>&#8220;$userfile.U<\/em>&#8221; and press tab to get auto completion!! It&#8217;s a breeze.<\/p><\/blockquote>\n<p>The trick is that you have to actually READ what is in front of you.\u00a0 The key here is to let PowerShell know that you are reading an xml file, and that is done by placing<strong> [xml]<\/strong> prior to getting the content.\u00a0 I missed that the first six times I read this and couldn\u2019t figure out why I wasn\u2019t getting the results I expected.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have written a lot of scripts that use .txt files to read or store data, but I have a need to read some information from an .xml file.\u00a0 This could be done by treating the file as a simple txt file, but it would require some pretty good filtering that is already a part&hellip; <a class=\"more-link\" href=\"https:\/\/blog.nukeitmike.com\/index.php\/2009\/09\/22\/using-xml-in-your-powershell-scripting\/\">Continue reading <span class=\"screen-reader-text\">Using XML in your PowerShell scripting<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[25],"tags":[135,154,201],"class_list":["post-207","post","type-post","status-publish","format-standard","hentry","category-scripting","tag-powershell","tag-scripting","tag-xml","entry"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pcW544-3l","_links":{"self":[{"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/posts\/207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/comments?post=207"}],"version-history":[{"count":0,"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/posts\/207\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/media?parent=207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/categories?post=207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/tags?post=207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}