Powershell load settings from an XML file

I have a Powershell script which I use to collect some data and email them to an inbox. In case this inbox changes I needed to be able to change the logon credentials easily. It wouldn’t be economical or secure to store the credentials in the script file and change the script each time the details were updated. Therefore I stored the credentials in an XML file and had the script load them at runtime.

First of all let me show you the XML file:

<?xml version="1.0"?>
<!-- This file contains settings for sending email with powershell -->
<Settings> 
  <Setting> 
	<Server>mail.example.com</Server> 
	<Port>25</Port> 
	<Ssl>true</Ssl> 
	<Username>domain\user</Username> 
	<Password>password</Password> 
	<From>example@example.com</From> 
	<To>example@example.com</To> 
  </Setting> 
</Settings>

As you can see from the above XML it is pretty simple and easily parsed with Powershell. Below is the Powershell code used to parse the XML and load the settings into variables for later use:

# Get settings from emailSettings.xml.
	[xml]$settings = get-content "$MyDir\emailSettings.xml"

# Assign the settings to local variables.
$server = $settings.Settings.Setting.Server
$port = $settings.Settings.Setting.port
if($settings.Settings.Setting.Ssl -eq "true")
{
	$ssl = $true
}
$username = $settings.Settings.Setting.Username
$password = $settings.Settings.Setting.Password
$from = $settings.Settings.Setting.From
$to = $settings.Settings.Setting.To

The variable $MyDir points to the directory the script is executing from allowing me to load extra files at runtime. You can get the current script directory by using the following code:

# Get the current script directory.
$MyDir = Split-Path $MyInvocation.MyCommand.Definition

You can see how easy it would be to modify the script in order to use it for other things.