Small Tips and Tricks – PowerShell – how to read content from XML file

fig : PowerShell - PowerShell to read content of XML file
fig : PowerShell - PowerShell to read content of XML file

Hi All,

Greetings for the day!!!

Today new learning for me so sharing 🙂

In this article we will discuss how to read XML file in PowerShell script

Background

Details

  • Its very similar how we read text file with PowerShell
  • We will use Get-Content CMDLET is used to read the XML file from given path
  • Get-Content CMDLET reads file line by line
  • Consider following XML file – Here I am explicitly taking .NET configuration file – App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <appSettings>
	<add key="ScriptPath" value="C:\\knoweldgejunction_powershell.ps1"/>
	<add key="UserName" value="knowledgejunction"/>
	<add key="Password" value="lifeisbeautiful"/>
   </appSettings>
</configuration>

  • So lets consider we need to read the username and password from above XML file – App.config
  • We will use Get-Content CMDLET as

[XML]$creds = Get-Content -Path "App.config"

  • Make sure we are type-casting output variable ($creds) with [XML]
  • Output of the above CMDLET is

xml                            configuration
---                            -------------
version="1.0" encoding="utf-8" configuration

  • We get very first node (top node) – <configuration>
  • As we have top node, we can loop through the child nodes or we can read individual nodes as well
  • Example –
$creds.configuration

o/p - 

appSettings
-----------
appSettings

  • Now here to read user name and password – we need to loop though all the keys under <appSettings> and get the respective values as
foreach($key in $creds.configuration.appSettings.add)
 { 
    if($key.key -eq "UserName"){
        $username = $key.value
        }#if

    if($key.key -eq "Password"){
        $password = $key.value
        }#if
 }#foreach

Complete Script

 $username=""
 $password=""

 [XML]$creds = Get-Content -Path "App.config"
 
 foreach($key in $creds.configuration.appSettings.add)
 { 
    if($key.key -eq "UserName"){
        $username = $key.value
        }#if

    if($key.key -eq "Password"){
        $password = $key.value
        }#if
 }#foreach

 Write-Host "UserName is"  $username
 Write-Host "Password is" $password
fig : PowerShell - PowerShell to read content of XML file
fig : PowerShell – PowerShell to read content of XML file

Thanks for reading 🙂 HAVE a FANTASTIC TIME AHEAD 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.