SharePoint 2010 and PowerShell: A Match Made In Automation Heaven

Written by Jeff Adkin, Microsoft Premier Field Engineer.

SharePoint 2010 and PowerShell: A Match Made In Automation HeavenWe have all seen it a thousand times before. We need to make a change across tons of SharePoint sites or pull a report on specific values that just are not available in Central Admin. Banging one’s head off of the proverbial wall starts to happen as we begin to wrestle with the idea that we may just need to go through each site manually . Energy levels start to drop as we come slowly to grips with all the ‘grunt’ work we are about to go through and no amount of espressos will bring the energy back up.

Now with the SharePoint 2010 Management Shell we have all the tools needed to quickly and efficiently automate even our most mundane tasks with relative ease. Anything that you could possibly want to do on a small or large scale can now be automated to save you from the many dreary hours of manual labor.

What can I automate in SharePoint 2010 Management Shell?

The simple answer is ‘Anything and everything’. Now there are of course tasks that it is just faster to click a button, but today we are discussing the large monotonous tasks. With the integration of PowerShell we can now automate anything we would normally do manually for our Farms, Sites and webs. We can even do this for any list or specific item type across our environment.

As an example: Shortly after the upgrade to SharePoint 2010 I was asked to compile a report that showed how many sites where now using the SharePoint 2010 interface vs. the SharePoint 2007 interface to determine how many people had adopted the new interface. With thousands of sites in front of me I realized very quickly that this is something I did not have the time or resources to do manually.

I opened the Management Shell and with one line of code (and 20 minutes of patience) I had the report generated. Well that was easy, I thought, and since I had additional time what should I do next. Then it dawned on me that this report would be called for again, and probably wanted on a weekly basis. I really did not want to have to format out the data every week so I broke up the script and added in the Output to CSV. Now a simple run of a script automated the report nicely every week for me, saving me huge amounts of time.

How do I automate on a large scale?

Let’s use the example above and walk through how we accomplished saving ourselves a ton of time and grief.. We started with a simple one line of code:

Get-SPSite -limit all | Get-SPWeb -limit all |Foreach { if ($_.UIVersion -eq 4){$Site=$_.Url;write-output $Site;} } >>UI4Sites.csv

What does this simple one line of code do? First off it enumerates every Site container, then checks every web object to see if the one value matches or not. If it equals UI 4 then the URL is taken and it is output to the CSV.

$exportfile = "UI4Sites.csv"
$Columns = "Web, Site Name, UIVersion, Language"
$Columns | out-File -filepath $exportfile
Get-SPSite -limit all | get-SPWeb -limit all |Foreach { if ($_.UIVersion -eq 4){$URL=$_.Url;$Title =$_.Title;$Ver=$_.UIVersion;$Lang=$_.UICulture.Displayname; $info="$url,$title,$Ver,$Lang" | out-file -filepath $exportfile -append ;} }

The one line code then morphed into a small script to automate the information I needed into a CSV with Column Headers. This script has grown since then into opening Excel and color coding Cells, but the basis is still the same.

Using the exact same logic you can retrieve whatever information you want, and change it as you see fit by checking for a specific value and if it matches then change it to equal what you want. Any large tasks can be automated easily, it is limited only to your imagination.

For more information on the PowerShell commands used please see the following links:

Comments (0)

Skip to main content