Patrick's SharePoint Blog

SharePoint's Booming world

Using PowerShell to deploy SharePoint Solutions (WSP)

Posted by Patrick Boom on May 31, 2010

The STSADM command line application worked well with previous versions of SharePoint. But the world is progressing and PowerShell will be the new administrative tool for SharePoint. In previous articles, I already showed some amazing powerful scripts that otherwise would require more lines of code. PowerShell offers some great advantages in large farms, as they can be run remotely on target machines, can be signed and therefore be controlled. Building up a repository of scripts or cmdlets to execute common tasks would be worthwhile in the long run.

First off an important thing to note here that this will only work with SharePoint 2010. There are no PowerShell snapins available for 2007, although you could create your own off course. And when run from a SharePoint 2010 Management Shell, the snapins are loaded automatically. But what if you just use the ‘normal’ console? Well, then you would have to register the snapin yourself. Fortunately, MS has already created the PowerShell script that is needed, located at:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\Config\PowerShell\Registration\SharePoint.ps1.

You could include this script in your scripts to be run first, or just include the command that registers the snapin:

Add-PSSnapin Microsoft.SharePoint.PowerShell

This article covers one of the most basic tasks one would do when administrating SharePoint: Deploy SharePoint solutions (WSP) and enable/disable features.

Working with Solutions

In the ‘old’ days (let us not forget that the stsadm is still there and we have a lot of SharePoint 2007 installations across the globe), the following stsadm command could be used to add a SharePoint solution to SharePoint:

stsadm –o addsolution –filename “D:\Deploy\MySharePointSolution.wsp

We used the following command to deploy the solution once installed to a specific web application:

stsadm –o deploysolution –name MySharePointSolution.wsp –url http://myspwebappp –allowgacdeployment –immediate

If we would upgrade an existing solution, we would use the following:

stsadm –o upgradesolution –name MySharePointSolution.wsp –filename “D:\Deploy\MySharePointSolution.wsp” -immediate

And finally, we used the following commands to retract and delete a specific solution from the web application:

stsadm –o retractsolution –name MySharePointSolution.wsp –url http://myspwebapp –immediate
stsadm –o deletesolution –name MySharePointSolution.wsp

Now, let us see how we could do above operations with PowerShell. For this, we use the following PowerShell commands:

Add-SPSolutionD:\Deploy\MySharePointSolution.wsp
Install-SPSolution –Identity MySharePointSolution.wsp –WebApplication http://myspwebapp –GACDeployment

If you would like to add the solution as sandboxed, you would use the Install-SPUserSolution command instead. To upgrade a solution, we specify which solution is to be updated and with which new solution file:

Update-SPSolution –Identity MySharePointSolution.wsp –LiteralPath “D:\Deploy\MySharePointSolution.wsp” –GacDeployment

To retract and remove a solution, we use the following commands:

Uninstall-SPSolution –Identity MySharePointSolution.wsp –WebApplication http://myspwebapp
Remove-SPSolution–Identity MySharePointSolution.wsp

Working with features

Similarly, commands exist for working with features. The stsadm equivalents:

stsadm –o activatefeature –name MyFeatureName –url http://myspwebapp
stsadm –o deactivatefeature –name MyFeatureName –url http://myspwebapp

Needless to say, there are easy equivalents in PowerShell:

Enable-SPFeature –Identity MyFeatureNameOrGuid –url http://myspwebapp
Disable-SPFeature –Identity MyFeatureNameOrGuid –url http://myspwebapp

As you can see, PowerShell will completely replace stsadm as the administrative command line tool for SharePoint. Better to start using it as the next version of SharePoint will not have a stsadm command line tool I suspect. To conclude, let us take a look at a script that enables a certain feature across all site collections and sites in a farm. As an example, I have taken the SharePoint Server Enterprise Site Features feature with ID 0806d127-06e6-447a-980e-2e90b03101b8.

Add-PSSnapin Microsoft.SharePoint.PowerShell
$WebApplications = Get-SPWebApplication

foreach ($webapp in $WebApplications) {
  $Id = $webapp.Id
  Write-Host “Processing web application $Id …”
  $sites = $webapp.Sites
  foreach ($site in $sites) {
    Write-Host Processing site $site.Id
    $webs = $site.AllWebs
    foreach ($web in $webs) {
      Write-Host Processing web $web.Title
      if ($web.Features["0806d127-06e6-447a-980e-2e90b03101b8"] -eq $null) {
        Enable-SPFeature -Identity 0806d127-06e6-447a-980e-2e90b03101b8 -url $web.Url -Confirm:$false
      } else {
        Disable-SPFeature -Identity 0806d127-06e6-447a-980e-2e90b03101b8 -url $web.Url -Confirm:$false
      }
    }
  }
}

Please note though that above script will work for small site collections. But for larger object hierarchies, you will need to include proper memory management (SPAssignment) to ensure proper release of memory.

As always, have fun coding.

About these ads

49 Responses to “Using PowerShell to deploy SharePoint Solutions (WSP)”

  1. [...] out this link from Patrick Boom – he also covers Retract, Delete and Deactivate. from → Uncategorized [...]

  2. [...] Below is some commands from Patrick’s SharePoint Blog [...]

  3. Kijk dit vind ik nou typisch iets wat waard is om te lezen. Toppie!

  4. [...] You could also – prior to adding the solution – check to see if the solution is already (deployed or added) and then (retract and) remove it. For deployment of the solution you need to have a sleep command ready, because adding is a job, that needs to finish and then use the deploy-spsolution with either the -allwebapplications or the webapp parameter. Something like this. [...]

  5. idowu ibrahim said

    pls help out. i did locate where to edit the wss_custom to change the policy…pls out asap. i will love if you can show the path to the bin directory. thanks.

  6. [...] http://patrickboom.wordpress.com/2010/05/31/using-powershell-to-deploy-sharepoint-solutions-wsp-2/ [...]

  7. [...] http://patrickboom.wordpress.com/2010/05/31/using-powershell-to-deploy-sharepoint-solutions-wsp-2/ [...]

  8. [...] Using PowerShell to deploy SharePoint Solutions (WSP) « Patrick’s SharePoint Blog [...]

  9. Anupam Kaul said

    This assumes that the script is executed on the same same server which has a Sharepoint installation. However how do you tackle this when you want to deploy to a SP Server remotely e.g. from a CI Server?

    Assume that I auto-build my solution on Server 1 and would like to deploy this solution to multiple environments (test, pre-prod, prod). Is it possible to give the URL parameter to the AddSPSolution cmdlet (and other subsequent cmdlets) so that I can deploy this to multiple environments without necessarily copying all the WSP’s and the script to deploy to each individual environment?

    Many thanks for any help here.

    • SharePoint WSP files can only deployed from a machine that has SharePoint installed. It is however possible to execute PowerShell scritps remotely, that is indicating a server on which the script should run from a central PC. This still requires the deliverables to be present on the server, but the script can be run remotely.

  10. [...] Using PowerShell to deploy SharePoint Solutions (WSP). [...]

  11. Sree said

    Hi Patrick,

    Can you help me to install, Deploy, Activate, Deactivate, Retract & Remove multiple wsp files by one script.

    Thanks,
    Sree

  12. [...] http://patrickboom.wordpress.com/2010/05/31/using-powershell-to-deploy-sharepoint-solutions-wsp-2/ [...]

  13. [...] referenced: http://patrickboom.wordpress.com/2010/05/31/using-powershell-to-deploy-sharepoint-solutions-wsp-2/ [...]

  14. [...] http://patrickboom.wordpress.com/2010/05/31/using-powershell-to-deploy-sharepoint-solutions-wsp-2/ [...]

  15. link building solutions…

    [...]Using PowerShell to deploy SharePoint Solutions (WSP) « Patrick's SharePoint Blog[...]…

  16. dhiya said

    This link is very useful to me

  17. best ping list wordpress…

    [...]Using PowerShell to deploy SharePoint Solutions (WSP) « Patrick's SharePoint Blog[...]…

  18. banyan resort…

    [...]Using PowerShell to deploy SharePoint Solutions (WSP) « Patrick's SharePoint Blog[...]…

  19. scripts, themes…

    [...]Using PowerShell to deploy SharePoint Solutions (WSP) « Patrick's SharePoint Blog[...]…

  20. [...] referan:http://patrickboom.wordpress.com/2010/05/31/using-powershell-to-deploy-sharepoint-solutions-wsp-2/ [...]

  21. AdventureBelle said

    When I try to run this cmdlet, I receive the following errors (only sometimes):

    Disabling feature: SPFeatureDefinition Name=FeatureDefinition/b89234a6-xxxx-xxxx-xxxx-200dd03e1fdd ( XXXStructures_XXXContentTypes , b89234a6-xxxx-xxxx-xxxx-200dd03e1fdd )
    VERBOSE: Leaving BeginProcessing Method of Disable-SPFeature.
    VERBOSE: Performing operation “Disable-SPFeature” on Target “Site Scope
    http://mysite|b89234a6-xxxx-xxxx-xxxx-200dd03e1fdd”.
    Disable-SPFeature : Cannot complete this action.
    Please try again.

    The part of the script that is producing this is…

    foreach ($feature in $features) {
    if ($feature -ne $null) {
    Write-Host “`nDisabling feature: ” $feature “(” $feature.DisplayName “,” $feature.ID “)”
    Disable-SPFeature $feature -url http://mysite -Confirm:$false -Force -verbose
    Start-Sleep -Seconds 10
    }

  22. Is there execadmsvcjobs PowerShell command for Sharepoint?

  23. Spot on with this write-up, I seriously believe
    this web site needs far more attention. I’ll probably be returning to read more, thanks for the information!

  24. Hiya! I just would like to give an enormous thumbs up for the nice information you’ve gotten right here on this post.
    I shall be coming back to your blog for more soon.

  25. Hello! I just would like to give an enormous thumbs up for the good
    information you have here on this post. I can be coming again to your blog for extra soon.

  26. Unquestionably believe that which you said.
    Your favorite justification seemed to be at the net the easiest factor to bear in mind
    of. I say to you, I certainly get annoyed at the same time as other folks
    think about worries that they plainly do not understand about.
    You controlled to hit the nail upon the top as
    well as outlined out the entire thing without having side-effects , other people can take a signal.
    Will likely be again to get more. Thanks

  27. Very nice post. I just stumbled upon your blog and wanted to say that I have truly enjoyed browsing
    your blog posts. In any case I’ll be subscribing to your rss feed and I hope you write again very soon!

  28. Hi there very nice site!! Guy .. Beautiful ..
    Wonderful .. I’ll bookmark your site and take the feeds also? I am glad to find numerous helpful information right here in the submit, we want work out extra strategies on this regard, thanks for sharing. . . . . .

  29. Very good post. I’m dealing with a few of these issues as well..

  30. I have been browsing online greater than 3 hours lately, but I by no means discovered any fascinating article like yours.
    It is beautiful price sufficient for me. In my view, if all site owners
    and bloggers made just right content material as you probably did, the internet will be much more useful than ever before.

  31. Howdy would you mind sharing which blog platform you’re using? I’m planning
    to start my own blog soon but I’m having a tough time choosing between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then most blogs and I’m
    looking for something completely unique. P.S My apologies for getting off-topic but I had to ask!

  32. Hello this is kind of of off topic but I was wanting to know if blogs use WYSIWYG editors or
    if you have to manually code with HTML. I’m starting
    a blog soon but have no coding know-how so
    I wanted to get advice from someone with experience.
    Any help would be enormously appreciated!

  33. […] Using PowerShell to deploy SharePoint Solutions (WSP) « Patrick’s SharePoint Blog […]

  34. Kevin said

    Have you eever considered about including a little bit more
    than just your articles? I mean, what you say is important and all.
    However imagine iff you added some great visuals or video clips too give
    your posts more, “pop”! Your content is excellent but with images and
    videos, this site could definitely be one of the greatest in its niche.
    Awesome blog!

  35. Wonderful post however I was wondering if you could write a
    litte more on this subject? I’d be very thankful if you could elaborate a little bit further.
    Bless you!

  36. yu said

    Thanks for the great article…. However I would like to mention that your command

    “stsadm –o deploysolution –name MySharePointSolution.wsp –url http://myspwebappp –allowgacdeployment –immediate”

    will NOT work for a WSP file that you have created from your site – ie. if you create a WSP file of a SITE using the “Save Site as a Template” option from the “SITE SETTINGS” section.

  37. home decor said

    whoah this weblog is grreat i love studying your posts.
    Keep up the grea work! You know, lots of individuals are searching around ffor this information, you can
    help them greatly.

  38. Cathern said

    Great blog here! Also your website loads up fast!
    What host are you using? Can I get your affiliate link to yourr host?
    I wish mmy web site loaded up as quickly as yours lol

  39. The cosmetic dentist crowns however will probably be white and
    look more like natural teeth. This could have a negative effect on your
    personal and professional life. If you are considering
    any of the procedures, you should consult with a reputed cosmetic dentist.

  40. I am really loving the theme/design of your website. Do you ever
    run into any web browser compatibility problems?
    A number of my blog audience have complained about my blog not operating correctly in
    Explorer but looks great in Firefox. Do you have any tips to help fix
    this problem?

  41. If attracting visitors to your site is not just a cause of major concern because you already
    buy them regularly, find out the number of of options are becoming your clients.
    I cut it as a result of a handful of paragraphs and upload it to my email list
    manager for sending tomorrow. This essentially lets Google now how relevant and informative your details
    is to the search query.

  42. […] Reference: http://patrickboom.wordpress.com/2010/05/31/using-powershell-to-deploy-sharepoint-solutions-wsp-2/ […]

  43. Good day! This is kind of off topic but I need some advice from an established blog.
    Is it very difficult to set up your own blog?

    I’m not very techincal but I can figure things out pretty quick.

    I’m thinking about setting up my own but I’m not sure where to
    begin. Do you have any points or suggestions?
    Thanks

  44. I was recommended this blog by my cousin. I’m not
    sure whether this post is written by him as no one else know such detailed about my problem.

    You are incredible! Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 224 other followers

%d bloggers like this: