In PowerShell, it is quite easy to work with XML, even if you are not very familiar with it. Because a lot of applications come with XML configuration files or output XML administrators should know how to work with XML from a scripting language. Since XML is just text, any scripting language can be used but PowerShell is better because it supports XML documents as a primitive data type.
Suppose you have the following XML in a file myxml.xml:
<books>
<book>
 <title>Windows PowerShell in Action</title>
 <description>One of the top books about PowerShell</description>
 <price>29.69</price>
</book>
<book>
 <title>Windows PowerShell: TFM</title>
 <description>Another great PowerShell book. A must have.</description>
 <price>34.49</price>
</book>
</books>
For PowerShell, this is just a text file. You need to get the contents of the file as one string to convert it to an XML datatype. That is actually a bit tricky. If you use get-content (like gc myxml.xml) you get a collection of strings back. You can join each item in the collection with a newline between each item with the [string]::Join() method. The full PowerShell command to read the XML as one string and save it in a variable is:
$books = [xml] [string]::join(”`n”, (gc myxml.xml))
If you do this a lot, you should create a function in your profile like so:
function get-xml($path)
{
 return [xml] [string]::join(”`n”, (gc $path))
}
With the above function, reading the myxml.xml file is a lot simpler:
$books = get-xml myxml.xml
Note that [string]::Join is not pure PowerShell but the .NET Framework. The string class has a method called Join and that method takes a character and a collection of strings as parameters. The character here is the newline. In PowerShell, that is `n (backtick n).
The $books variable now stores the xml data. If you type $books.gettype() you will see it is not a string but a System.Xml.XmlDocument type. If you just type $books on the command line, you get the following result:
books
—–
books
The root element is returned and that is basically it. If you type $books.books you will get the book elements back. To see each book, just type $books.books.book. The following result is shown:
Great, now you can do stuff like sorting, grouping or measure-object to calculate the sum, average, whatever you want. For example:
$books.books.book | measure-object -Property price -Sum -Average | ft
Of course, you can also modify the XML. If you want to change the price of the first book you could use: $books.books.book[0].price=”20″. To save the XML back to a file, use $books.save(”path_to_file”).
Now let’s put this into practice with an administration example. If you work with Microsoft products, you have seen that many use XML. SharePoint is one of those. In SharePoint, the command line tool stsadm.exe outputs XML when you ask information about users, sites, and so on… For example, when you type the following command:
stsadm -o enumsites -url http://moss.newtech.local
you get something back like:
<Sites Count=”4″>
 <Site Url=”http://moss.newtech.local” Owner=”NEWTECH\administrator” ContentDatabase=”WSS_Content_b07a8403aac04322be509
09ed6dc5a13″ StorageUsedMB=”2.3″ StorageWarningMB=”0″ StorageMaxMB=”0″ />
<Site Url=”http://moss.newtech.local/mysites” Owner=”NEWTECH\administrator” ContentDatabase=”WSS_Content_b07a8403aac04
322be50909ed6dc5a13″ StorageUsedMB=”0.2″ StorageWarningMB=”0″ StorageMaxMB=”0″ />
<Site Url=”http://moss.newtech.local/personal/administrator” Owner=”NEWTECH\administrator” ContentDatabase=”WSS_Conten
t_b07a8403aac04322be50909ed6dc5a13″ StorageUsedMB=”0.4″ StorageWarningMB=”80″ StorageMaxMB=”100″ />
<Site Url=”http://moss.newtech.local/personal/geba” Owner=”NEWTECH\geba” ContentDatabase=”WSS_Content_b07a8403aac04322
be50909ed6dc5a13″ StorageUsedMB=”0.5″ StorageWarningMB=”80″ StorageMaxMB=”100″ />
You can hardly call the above information administrator friendly. It would be much better if you could display the data in a tabular format, with filters, sorts etc… In this case, we don’t have to grab the XML data from a file but from the output of the stsadm command. Let’s see what we can do:
- $rawdata=stsadm -o enumsites -url http://moss.newtech.local
- $sitesxml=[XML]$rawdata
- $sitesxml.sites.site | ft url,owner,storageusedmb -autosize
The first line grabs the output from stsadm as a string and puts it in a variable. No need to use [string]::Join here. The second line converts the XML string into an XML type. The third line gets the data from the site elements and formats the url, owner and storageusedmb properties in a nice table. If you check the raw XML coming from the stsadm command, you will notice that my SharePoint personal sites are in the same web application as the portal site collection. To only get the personal sites, you can use:
$sitesxml.sites.site | ? {$_.url -match “/personal/”} | ft url,owner,storageusedmb -autosize
? is an alias for Where-Object. Use man where-detailed for more info.
stsadm has many other enum commands that output XML so you can use the techniques here to solve other management issues. Another cool addition would be to combine this with PowerGadgets to visualize the results with a graph.

3 Comments
Can’t run stsadm in PowerShell. When I try running STSADM.EXE from within PowerShell, as shown in your example, I get an error:
The term ’stsadm.exe’ is not recognized ….
I had previously issued a cd ‘C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN’ to make sure I was in the correct directory.
What else needs to be done to allow direct execution of stsadm.exe from within PowerShell?
Fred,
It should work without doing anything special. I also have the path where stsadm.exe is in my system path variable. But that’s it. And that’s on a plain install of SharePoint with PowerShell added. Nothing else….
To get stsadm to work in PowerShell, I had to add the following to profile.ps1, located in directory C:\WINDOWS\system32\windowspowershell\v1.0\.
Set-Alias -Name stsadm -Value $env:CommonProgramFiles”\Microsoft Shared\Web Server Extensions\12\BIN\STSADM.EXE”
Hope this helps others struggling with the same error.
2 Trackbacks/Pingbacks
[…] Link to pshell.info / SharePoint, PowerShell and XML Share this post: Email it! | bookmark it! | digg it! | reddit! Published Thursday, April 05, 2007 12:08 AM by Richard Filed Under: Programming, Other Stuff […]
STSADM Command References…
Quick Outline for more detailed items on STSADM, especially with PowerShell goodness TechNet Powerful…
Post a Comment