11/14/2015

Sorting Hashtables and SharePoint Properties Lists with PowerShell

 

Sometimes the obvious just does not work… I was doing some work with SharePoint publishing sites, trying to find some of the data in the SPWeb AllProperties property. AllProperties looks like a collection of Names and Values. Actually it is Hashtable object.

My first attempt looked like this:

    $psite.AllWebs[0].AllProperties | sort -Property name

image

The output was random, not sorted as expected. After too long messing with trial and error, I did a web search and found a TechNet "Windows PowerShell Tip of the Week" article with the answer. Hashtable objects and sort just don't work together. We need to get an enumerator for the hash table and work with that…

   $psite.AllWebs[0].AllProperties.GetEnumerator() | sort -Property name

image

That's the output I was looking for!

If you pipe both of the above to Get-Member you will see that the first just returns a Hashtable object that includes a collection of Keys and a collection of Values. The second one, using GetEnumerator, returns a single DictionaryEntry object (one at a time through the pipeline) with a Key and a Value property. Now we have something to sort on!

image

.

No comments:

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.