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 Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

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

%d bloggers like this: