In Part 1 of this series we have explored PowerShell basics and how to use it with SharePoint 2010.
Now let’s discover how to write PowerShell script to access SharePoint objects and perform some operations
Create Site collection
The following PowerShell script will Create 10 Site collections under sites managed path from the team site template
We used New-SPSite cmdlet inside for loop and passed as parameters the site URL, Site owner, template id and site language
cmdlets used: New-SPSite
Add items to Announcements list
our second script is to add items to announcements list which can be helpful if you want to add a lot of items in a list for demo or development or testing purposes
In this script we retrieved the web object then through object properties we retrieved the list by its title and through for loop we added new item in each loop.
cmdlets used: Get-SPSite, Get-SPWeb
I never was a PowerShell or scripting guy, it is known that the scripting and PowerShell is made for Administrators and infrastructure guys.
With the release of SharePoint 2010 I noticed that Microsoft has added great support for PowerShell in SharePoint 2010. Now All SharePoint 2010 administration tasks can be performed with PowerShell.
SharePoint 2010 Management Shell – command prompt with SharePoint cmdlets loaded
In SharePoint 2010 PowerShell cmdlets there is 500+ cmdlets which supports all the Administration features through the UI and more.
PowerShell is more powerful than stsadm from functionality and performance perspectives.
For example running a stsadm command to activate feature on 5000 site collection can take more than 12 hours, but if you used the equivalent PowerShell script it will take around 30 minutes to finish the same task.
Also PowerShell supports connecting to a remote server and executing PowerShell cmdlets.
For PowerShell basics check this post PowerShell 2010: Basic Discovery - Zach Rosenfield's SharePoint Blog
Sample SharePoint PowerShell commands
Get-SPSite – this command will return list of all available site collections
Piping Get-SPSite cmdlets with Get-SPWeb will return all available webs in all site collections
Following sample gets available webs and shows their URL and template name
Get-spsite | get-spweb | Select url,webtemplate
Next sample will show how to get available services on your farm.
$farm = Get-SPFarm
$farm.Services | Select TypeName,Status
Note: All SharePoint PowerShell cmdlets their noun starts with SP
You can retrieve available SharePoint cmdlets by executing the following command
Get-command -noun sp*
Hope this post helps you start playing with PowerShell in your SharePoint farm
I will be posting later on sample scripts to create list items and site collections
also About great third party tools helps you in PowerShell scripts development
Free PowerShell books helps you get started
Schweizer IT Professional und TechNet Blog : Free Windows PowerShell workbook: server administration
Master-PowerShell | With Dr. Tobias Weltner - PowerShell.com
See also:
PowerShell and SharePoint 2010 love story – Part 2
PowerShell 2010: Basic Discovery - Zach Rosenfield's SharePoint Blog
PowerShell 2010: Basic SharePoint Cmdlets - Zach Rosenfield's SharePoint Blog
SP 2010: Intro to PowerShell Part 3 - Zach Rosenfield's SharePoint Blog
SP 2010: Intro to PowerShell Part 4 - Zach Rosenfield's SharePoint Blog
SharePoint 2010 Beta: ThreadOptions error - Zach Rosenfield's SharePoint Blog