Thursday, July 14, 2011

Deploy SharePoint Solutions (WSP) using PowerShell

PowerShell offers some great advantages while Deploying Solution (WSP) on SharePoint 2010 farm. Microsoft 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\MySPSolution.wsp“
We used the following command to deploy the solution once installed to a specific web application:
stsadm –o deploysolution –name MySPSolution.wsp –url http://myspwebappp –allowgacdeployment –immediate
If we would upgrade an existing solution, we would use the following:
stsadm –o upgradesolution –name MySPSolution.wsp –filename “D:\Deploy\MySPSolution.wsp” -immediate
And finally, we used the following commands to retract and delete a specific solution from the web application:
stsadm –o retractsolution –name MySPSolution.wsp –url http://myspwebapp –immediate
stsadm –o deletesolution –name MySPSolution.wsp
Now, let us see how we could do above operations with PowerShell. For this, we use the following PowerShell commands:
Add-SPSolution “D:\Deploy\MySPSolution.wsp“
Install-SPSolution –Identity MySPSolution.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 MySPSolution.wsp –LiteralPath “D:\Deploy\MySPSolution.wsp” –GacDeployment
To retract and remove a solution, we use the following commands:
Uninstall-SPSolution –Identity MySPSolution.wsp –WebApplication http://myspwebapp
Remove-SPSolution–Identity MySPSolution.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.