Using PowerShell to read xml-files

A couple of months ago a colleague and I decided to try to build a demo-environment for SharePoint. We wanted to be able to take a new fresh installed Windows 2003 (or 2008) server, and just run a PowerShell script and then the environment should be up and running. The environment should of course include sample data, sites, and last but not least a number of users.
In order to do this we first had to learn PowerShell. Always fun to learn new things! This post is about how we use PowerShell to read the userdata from the xml-file.

Step 1: Download PowerShell from here:
PowerShell Download
Step 2: install it.

Lesson 1:
The first thing I wanted to do was to execute a script that we’ve made ourselves.
Here’s the result:

PS C:\Tmp> .\TestScript.ps1
File C:\Tmp\TestScript.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details.
At line:1 char:16
+ .\TestScript.ps1 <<<<

Ok, that was clearly not the result I wanted! Fortunate for me, the error message actually says what I need to do.
PS C:\Tmp> Get-Help about_signing

That gave me a lot of information, mainly this:

Set-ExecutionPolicy <policy-name>

Next command to try:
PS C:\Tmp> Set-ExecutionPolicy -ExecutionPolicy Unrestricted

Voila! We can now run our scripts without signing them.

Lesson 2:
Read data from an XML-file.
The XML-file that I’m going to read from has the following structure:
Reading data from an XML-file is really easy in PowerShell! Use this command to load the file into an variable:
PS C:\Tmp> [xml]$userfile = Get-Content Accounts.xml

When the xml-file is loaded you can type “$userfile.U” and press tab to get auto completion!! It’s a breeze.

Lesson 3:

After that it’s time to do a foreach-loop to write the name of the users on the screen:
foreach( $user in $userfile.Users.User)

    Write-Host $user.Name

Our script file now looks like this:
[xml]$userfile = Get-Content Accounts.xml

foreach( $user in $userfile.Users.User)
    write-host $user.Name

And when we execute it we get the following written on the screen:
PS C:\Tmp> .\TestScript.ps1
PS C:\Tmp>

Super easy!! Next time we’re going to create AD-accounts for the users in the xml-file. Till then: Enjoy!

Comments (3)

  1. k6497 says:

    Could you show how to traverse the XML file to get to a sibling node level? I can use the lesson 3 section to get part of what I need, but in my file there is another level that would put something like interest on the same level as user from your example.  I am actually working with the DFSR health report xml that has header and members as siblings under dfsreplication report.

  2. Kevin Morgan (kaylus) at Sobster says:

    I know this is dragging up a dead pony but I wanted to answer K6497's question:

    If you want to know how to work properly with XML objects in Powershell you can traverse the XML item you create simply by referencing the locations available at each level. I know that sounds tricky, but take the example above and type:


    Into powershell, you will see that it says "Users". Try then, $userfile.Users, and you will see "User" as a node. Now edit the XML and add a sibling node to user, then check again.

    The best way to find out how to manipulate beyond this is to (using the above example) use:

    $userfile | Get-Member

    These members are specific to the item passed so the above will have XML and XML-file based members, so you can also pipe:

    $userfile.Users | Get-Member

    To see members specific to the XML object itself.