Get Started Managing SharePoint 2010 with PowerShell Cmdlets

Get Started Managing SharePoint 2010 with PowerShell Cmdlets

  • Comments 5
  • Likes

 

Summary: Scripting Guys guest blogger Niklas Goude shares essential how-to information about using the Windows PowerShell cmdlets to manage SharePoint 2010.

 

Hey, Scripting Guy! QuestionHey, Scripting Guy! I am interested in getting started with using Windows PowerShell cmdlets to manage SharePoint 2010. Do you have any words of wisdom?

-- GN

 

Hey, Scripting Guy! Answer Hello GN,

Microsoft Scripting Guy Ed Wilson here. Yes I have words of wisdom: “Ask the guy who wrote the book.” We have Niklas Goude with us this week to share his wealth of knowledge. Niklas, we turn the blog over to you this week.

Niklas Goude is a Windows PowerShell MVP working at Enfo Zipper in Stockholm, Sweden. Niklas has extensive experience in automating and implementing SharePoint environments using Windows PowerShell. He has written a Windows PowerShell book for Swedish IT pros, powershell.se, and is currently co-authoring a book with Mattias Karlsson titled, PowerShell for Microsoft SharePoint 2010 Administrators, which will be published in English by McGraw-Hill in October 2010. Parts of this post are taken from Chapters 3 and 4 of that book.

Niklas also runs the blog, powershell.nu, where he shares scripts, examples, and solutions for administrative tasks in Windows environments through Windows PowerShell.

 

Getting Started

Windows SharePoint is one of the fastest growing Microsoft products in history, and it is quickly becoming mission critical for many companies around the world. Whereas SharePoint 2007 was a really cool product with an automation API, its use for automation purposes was a bit complicated for the average SharePoint administrator. This is where the inclusion of Windows PowerShell as a management tool for SharePoint 2010 comes in to play.

In SharePoint 2010, you can start Windows PowerShell through the SharePoint 2010 Management Shell. The shell runs the SharePoint.ps1 script at startup and executes the following code:

$ver = $host | select version
if ($ver.Version.Major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}
Add-PsSnapin Microsoft.SharePoint.PowerShell
Set-location $home

The code in the preceding example stores the host’s version in a variable, and if the major version is greater than one (if you are running Windows PowerShell 2.0), the ThreadOptions property is set to “ReuseThread”, which runs each line, function, or script on the same thread. When working with the SharePoint object model using Windows PowerShell, running code on separate threads can cause memory leaks, but commands running on the same thread have a smaller chance of doing so. This is not only because some SharePoint objects still use unmanaged code, but also the way memory is allocated to those objects. Next, the SharePoint snap-in is loaded (Microsoft .NET Framework assemblies that may contain custom Windows PowerShell cmdlets).

The SharePoint 2010 Cmdlets

The SharePoint 2010 snap-in for Windows PowerShell contains more than 500 cmdlets that you can use to perform a large variety of administrative tasks. Let’s see how we can list all the SharePoint cmdlets using the Get-Command cmdlet. Get-Command returns basic information about cmdlets and other elements of Windows PowerShell commands, such as functions, aliases, filters, scripts, and applications. All nouns of the SharePoint 2010 cmdlets start with SP. Knowing this, we can use the –noun parameter of the Get-Command cmdlet followed by SP*:

PS > Get-Command -noun SP*

The results of this command are shown in the following image.

Image of results of this command

The list of cmdlets returned is pretty long. You can also use Get-Command to find specific SharePoint 2010 cmdlets. For example, if you want to find all cmdlets that are used to manage site collections, you can simply type:

PS > Get-Command -Noun SPSite

This will produce the following results:

CommandType                 Name                              Definition

------------------                  --------                             ------------

Cmdlet                              Backup-SPSite                Backup-SPSite [-Identity] <SPSitePip

Cmdlet                              Get-SPSite                      Get-SPSite [-Limit <String>] [-WebAp

Cmdlet                              Move-SPSite                   Move-SPSite [-Identity] <SPSitePipeB

Cmdlet                              New-SPSite                     New-SPSite [-Url] <String> [-Languag

Cmdlet                              Remove-SPSite               Remove-SPSite [-Identity] <SPSitePip

Cmdlet                              Restore-SPSite                Restore-SPSite [-Identity] <String>

Cmdlet                              Set-SPSite                       Set-SPSite [-Identity] <SPSitePipeBi
Working with SharePoint 2010 Cmdlets

Let’s see what we can do with the Get-SPSite cmdlet. Typing the cmdlet in Windows PowerShell returns the site collections available:

PS > Get-SPSite

Url

---

http://spserver

Notice how the command returns the site collections URL property, although the returned objects have a lot more properties. The properties displayed by default are controlled by a set of formatting files. Windows PowerShell includes 10 formatting files, and SharePoint 2010 comes with 13 additional formatting files that are used to generate a default display of various .NET Framework objects.

We can display additional properties using the Select-Object cmdlet. In the example below, we use the –Identity parameter supported by the Get-SPSite cmdlet to retrieve a specific site collection and pipe the object to the Select-Object cmdlet:

PS > Get-SPSite -Identity http://SPServer | Select-Object -Property Url, Zone, Port

Url Zone Port

--- ---- ----

http://spserver Default 80

It’s also possible to change specific properties of a site collection. First let’s see how to we can add a secondary contact to the site collection using the Set-SPSite cmdlet:

PS > Get-SPSite -Identity http://SPServer | 
>> Set-SPSite -SecondaryOwnerAlias domain\user

If we use the Select-Object cmdlet again and display the SecondaryContact property, we’ll see that a secondary contact is added to the site collection:

PS > Get-SPSite -Identity http://SPServer | Select SecondaryContact

SecondaryContact

----------------

Domain\user

You can also store an object of the type SPSite in a variable and set the SecondaryContact property. The property requires an object of the type Microsoft.SharePoint.SPUser, which is just the type of object that the Get-SPUser cmdlet returns. Note that the user has to exist in the site collection:

PS > $spSite = Get-SPSite -Identity http://SPServer
PS > $spSite.SecondaryContact = 
>> (Get-SPUser -Web http://SPServer -Identity domain\user)

What if you want to add a user that exists in Active Directory but does not exist in the site collection? Simply use the New-SPUser cmdlet to add a user to a site collection and then add the object to the SecondaryContact property:

PS > $spUser = New-SPUser -Web http://SPServer -UserAlias domain\newuser
PS > $spSite.SecondaryContact = $spUser

When we are done with the object stored in the $spSite variable, it’s important to dispose of it correctly. One way of doing this is by calling the Dispose() method:

PS > $spSite.Dispose()

Why do we have to dispose of the object? Well, SPWeb, SPSite, and SPSiteAdministration objects can sometimes take up large amounts of memory, so using any of these objects in Windows PowerShell requires proper memory management. Normally, instances of these objects obtained through cmdlets such as Get-SPSite are disposed of automatically at the end of the pipeline, but this does not happen to instances stored in variables. You can dispose of objects using the Dispose() method as demonstrated in the preceding example, or you can use the Start-SPAssignment and Stop-SPAssignment cmdlets that were introduced in SharePoint 2010 to spare scripters the need to dispose of such objects individually.

Summary

In this post, we have seen examples about how to find and work with the SharePoint 2010 cmdlets. There is, of course, a lot more cool stuff that you can do when using Windows PowerShell to automate your SharePoint 2010 environment, such as managing content databases, web applications, and sites. We also took a brief look at how we can work with site collections in SharePoint 2010 with examples about retrieving and modifying site collections. It’s also possible to create new site collections, back up and restore site collections, and move and remove site collections.

GN, that is all there is to getting started with SharePoint 2010 and Windows PowerShell. Guest Blogger Week with Niklas Goude will continue tomorrow.

We would love for you to follow us on Twitter and Facebook. If you have any questions, send email to us at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • I have got some issues so want to discuss.

    I have got some requirement

    Script i need to create is talks to sharepoint using powershell.

    Input is config,xml file

    It takes two parameters

    1st is Server Name and second id Config file.

    Script firstly is going to check if the Server exist.If it does not exist it will display error to user.

    Then it asks for login credentils of the user.If it's correct then moves forward to check the entries in the config.list to check if there are any duplicate entries.

    Quesions:

    I am a newbie to powershell and sharepoint , so please bear with me even for small clarifications.

    1. i am trying to use the $get-spweb function to check if server exists.

    I am using the powergui editor , but it's giving me error get-spweb is not a correct commnd of powershell.

    I also tried to add Microsoft.Sharepoint.Powershell snapin to the powergui editor code but it's telling me

    Microsoft.Sharepoint.Powershell is not installed on your machine.

    Please let me know what are the softwares needs to have to run the get-spweb command in powergui editor.

    Also please let me know the idea of code how above requirement can be fulfillled

  • Hi,

    In order to use the SharePoint 2010 cmdlets (Get-SPWeb, Get-SPSite and alot more..) demonstrated in this post you have to have SharePoint 2010 installed on a Server in your environment and you have to run the cmdlets (Get-SPWeb, Get-SPSite..) from the Server, either log in on the server and run the commands or use the remoting features available in PowerShell V2.

    Here's a great page that explains how to use SharePoint 2010 with PowerShell Remoting.

    blogs.msdn.com/.../sharepoint-2010-with-windows-powershell-remoting-step-by-step.aspx

    Regards

    Niklas

  • Thanks Niklas for your reply..i really appreciate...-:)..

    Actually i am having a script requirement that need to talk to sharepoint.

    Script takes two command line arguments: Ip address of server or Server name , Config.xml file.

    User is asked for username and password to login to above server which ip address or server name is given.

    If user name and password are incorrect for logging into Sharepoint then error is shown to user.(Please let me know how i get this part handled).

    If Login is Successful , then List in Config.xml file is checked to find the duplicate entries.

    If duplicate entries are found error is shown to user.

    If no duplicate entries are found then list in Config file is compared with the list in Sharepoint to check if entires in both Config.xml file list and Sharpeoint list are same.If they are not same error is shown to the user.

    I need to Run the script on the Central admin and talk to any Web Front End in the Farm.

    Please let me know the script that will do the required task.

  • Do I (or the authentication used to connect to the sharepoint site) need admin rights to the sharepoint server, or just sharepoint site? Do I have to be looged in to the sharepoint server to run the script? Can I use the wmi tools to run them remotely? How do I pass the credentials through the scripts? Thanks.

  • i want a SharePoint management command list which fully described all type of command functioning