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:
<Users>
  <User>
    <Name>Kalle</Name>
 
</User>
  <User>
    <Name>Becker</Name>
  </User>
</Users>
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
Kalle
Becker
PS C:\Tmp>

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