Create an XML Variable in PowerShell

Introduction

In this tutorial we're going to learn how to create an XML variable in PowerShell. A what? Yes, you read correctly, PowerShell supports XML as a native data type. Let's do this.

Creating Your XML Variable

Creating an XML variable is almost exactly the same as creating a string variable with one syntactical difference; You need to cast the string using the XML type:

Example

$theXmlVariable = [XML] "

Tove
Jani
Reminder
Don't forget me this weekend!

Copy and Try it

PowerShell automatically creates each child node as a property, so to get the to property we could type the following into our shell:

Example

   echo $theXmlVariable.note.to`

Copy and Try it

Or the note's body:

Example

    echo $theXmlVariable.note.body
 

Copy and Try it

Pretty neat huh? This type of functionality has a lot of potential once you start getting into XML Web Services and Data Mining.