PowerShell Basics 102 – SharePoint

After learning the basics in the first post – PowerShell Basics 101 , the aim is to use this new found knowledge to do some interesting things with SharePoint.

Connecting to a SharePoint Site

If you open the SharePoint 2010 Management Console, all the appropriate assemblies for SharePoint will be loaded for you.

To instantiate a Site object, all we need to do is the following:

$site = New-Object Microsoft.SharePoint.SPSite(“http://localhost”)

This will create a SPSite variable.

Viewing SharePoint Properties

Now that we have this variable, we can display the properties of an SPSite.  For example, an SPSite has a Url and a Usage property.




The Url property will give you the (obviously) the Url.  The Usage property is a little more interesting, since it is a more complex data type.  It will show you Storage, Bandwidth, Visits etc.

What if you only want the Storage part of the Usage property?


Showing SharePoint Farm Properties

To get the access to the Farm properties, we need an SPFarm object.  We can get this from an SPWebApplication object which is a member of the SPSite object that we have.  Fun isn’t it? :).

$farm = $site.WebApplication.Farm

The SPFarm object has a Properties hashtable that we can enumerate and display the values.  A Hashtable has keys to get the values.  So we can enumerate the Keys and with each key, get the corresponding Value.

foreach ($key in $farm.Properties.Keys)

That will show us all the Properties that we have.  Now lets say we want to display the Key and the Value.  We can have the following ForEach script:

foreach ($key in $farm.Properties.Keys)
  $key + ‘ – ‘ + $farm.Properties[$key]

We now have all the properties we want to see from our Farm.

Note that if you do something like the following, it will set the value of the farm property, which could be quite dangerous.


That’s all for lesson 102.

Leave a Reply