SharePoint 2010 Cookbook: How to Use PowerShell to Browse the SharePoint Object Model

While developing custom code for SharePoint, I’ve often run across situations where I need to quickly find the list schema, column internal name, or just the GUID of an item so I can make a decision and continue designing the code accordingly. In pre-PowerShell days I used to write NUnit code, browse to the List Settings page, or sometimes just engaged in some plain old-fashioned debugging with breakpoint, followed browsing the object in Visual Studio IDE’s watch window to find what I needed.

The good news is that with the introduction to PowerShell in general, and PowerShell for SharePoint in particular, it’s now much simpler to perform these rudimentary tasks.

Challenge:

How do I browse through the SharePoint object model using SharePoint PowerShell commands?

 

Solution:

Retrieving a value from the object model boils down to traversing the object model. The hierarchy is to get to a site collection object – SPSite – from which we get to a site object – SPWeb -from which we can get to a list collection object – SPListCollection and so on and so forth. A complete hierarchy can be seen here at the MSDN site.

Here are some steps to get you going.

Run SharePoint 2010 PowerShell console window (How?).

1- Get SPSite object in a variable. I will use the SharePoint PowerShell cmdlet Get-SPSite to get my object.

 

Or you can enumerate all sites and then pick the one you are interested in.

 

2- Get the SPWeb object in a variable. Note that I am using the object saved in $s variable above and printing out the website title. I can use many other methods and properties exposed SPWeb object.

In case you can’t remember the method or property names, just run the following command to list all public methods. And then run the above command.

Run the command without the “-membertype” switch to list all the methods and properties.

3- Get SPList object.

Caution: Don’t try to execute $w.Lists as it will go through each list in the list collection which could be very time consuming.

 

4- Dump the whole object or just a property to a file for later use

 

And finally the list schema.xml to review in IE browser ($l.SchemaXML).

 

Notes:

From the SharePoint PowerShell console window type Get-Help Get-SPSite to find out the usage and other options.

 

See also: