Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint, Office 365, Mobile Development, and other Microsoft products, as well as some discussion of general programming related concepts.

Adding and Deploying Solutions with PowerShell in SharePoint 2010

Visual Studio 2010 makes it really easy to add and deploy solutions when you are developing, but you may eventually want to deploy those solution packages elsewhere right? We can still use stsadm, but that is effectively considered deprecated now in favor of PowerShell. In the past to add a solution, we used an stsadm command like the one below. In today’s example, we’ll be working with a package called SharePointProject2.wsp on my server named sp2010.

stsadm –o addsolution –name SharePointProject2.wsp

To get started with PowerShell, run the SharePoint 2010 Management Console located in your Microsoft SharePoint 2010 Products folder on your start menu. This automatically loads the Microsoft.SharePoint.PowerShell snappin so that we can execute SharePoint commands. To install a solution we use the Add-SPSolution command. If you are using a Sandboxed solution you would use Add-SPUserSolution instead. It takes just one parameter, –literalpath, which is the path to the solution file. One thing that is different is that you must specify the full path to the file for some reason. I haven’t been able to get it to take a path to the solution in the current folder even if I make use of .\ before the filename. Here is an example.

In this case you don’t actually have to type –literalpath before the parameter. This is what it looks like when executed. You can see that it displays the id of the solution along with its deployment status.

Now we need to deploy the solution. In the past, we used an stsadm command like the following.

We would also follow this up with a call to stsadm with the execadmsvcjobs operation. To do the same operation in PowerShell, we use the Install-SPSolution command (again use Install-SPUserSolution for Sandboxed solutions). You can use the Get-Help command (i.e.: Get-Help Install-SPSolution) to get more information on a command but it’s not always obvious what it is expecting as you can see below. That is why I am writing the post today.

The first parameter you need is the –Identity parameter. This is the name of the solution package (i.e.: MySolution.wsp). Depending on if you are using the GAC or Code Access Security, you will specify either –GACDeployment or –CASPolicies. You then need to specify a specific web application using the –WebApplication parameter or –AllWebApplications to deploy it to all web applications (assuming the manifest allows it). If you need to force the deployment, you can still use the –Force command. Here is what an install command might look like.

I’ll point out that executing this command actually does do the deployment operation. You don’t have to fire off something to execute a job later like you did with stsadm.

You might want to update your solution, so we’ll talk about how to do that as well. Here is what your stsadm command might have looked like in WSS3. Which would also be followed up with an execadmsvcjobs operation.

The upgrade solution syntax is similar to the others. We just have to specify an identity and a literalpath with the Update-SPSolution command. The identity is the name of the package on the server to upgrade and the literalpath is the full path to the new solution package on the file system. Here is what that might look like.

We’ve talked about everything else, so we’ll finish it up by talking about retraction and removal. To retract a solution we use the Uninstall-SPSolution command. By now you are probably noticing a pattern in the way things are named. Install –> Deploys, Uninstall –> Retracts. It also just uses the -Identity parameter and the –WebApplication parameter. You can also use the –AllWebApplications parameter to retract it from all web applications. Many of these commands may prompt you with an “Are you sure?” type prompt. You can skip this prompt by adding a –confirm parameter. Here is what it looks like.

Lastly, to remove the package from the solution store, we use the Remove-SPSolution command. It just takes the name of the package.

Remove-SPSolution –Identity SharePointProject2.wsp

I hope this helps. If you’re like me, it’s one thing to see the docs on something, but I like to see real examples. There aren’t any examples in the Get-Help command yet. This should cover all of the common commands that you probably used to used with stsadm in regards to solution deployment. The nice thing is that you can script these things together very easily and create highly flexible PowerShell scripts. Expect a few more posts soon on the basics of working with PowerShell and SharePoint 2010.

@Chris @ynuska If your project doesn't have any features scoped for a specific web application, use the -AllWebApplications parameter instead of the -Url parameter.

July 26, 2010 3:11 PM

AidanQ
said:

If the solution has no web application resources it is considered a global solution and does not actually need to target an web applications. Remove the -AllWebApplicaitons and this global solution will be deployed globally to the farm and will be available to all web applications

Brad
said:

Very helpful. Thanks!

November 11, 2010 3:27 PM

Tania
said:

This PowerShell concept is really different and very interesting to read. The tips gave me many useful things. But i have one doubt that is this resource has no web application can you explain about it? Thanks...

@Leo Check your solution gallery in Central Administration to verify that the solution was installed. You won't see your web part anywhere until you activate the site collection feature associated with it. You can use a separate PowerShell command to activate features (more info can be found on this site).

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?

@Anumpam That is correct. To my knowledge, the solution packages always need to be on the target machine. It is possible to execute these commands using PowerShell remoting though, but I haven't looked into the details.

May 30, 2011 9:22 PM

ThinLizzy7
said:

Tried to use but keep getting error, any ideas?

Add-SPSolution : This solution contains one or more assemblies targeted for the

global assembly cache. You should use a strong name for any assembly that will

@ThinLizzy7 This means that the assembly you built is not strongly named. You can specify a strong name key in the properties of your project in Visual Studio. Rebuild the package and then you should be able to deploy it.

Corey, I want to deploy a new version of a Web Part that is already deployed as version 1.1.0.0. So I want version 1.0.0.0 to still exist and I want the new version 1.1.0.0 to also be in the GAC. If I do an Update-SPSolution it overwrites the version that is on the application and breaks it. How can I do deploy a new revision and not break what I have already deployed?

@Dave If you are going to keep the original version, you will have to build a new solution package that has a different name and guid. Since you are using Update, it's going to replace whatever is out there. Now you could also possibly get around this by just creating a new .webpart file with a different name.

smw
said:

How about retracting two solutions back to back? If I issue a batch command to remove all my solutions how can I accomplish it without getting the 'A deployment or retraction is already under way for the solution' error?

Pingback from SharePoint 2010 &#8211; How to activated and deactivated Features with PowerShell | SharePoint - How To

December 29, 2011 4:25 AM

Rajan
said:

I have a different issue. I uninstalled a webparts solution. But not able to retarct it. In Manage farm solutions, I can see the status as "retracting" always. Not sure why this is happening. I want to remove the solution completely from sharepoint.

While clicking that particular webpart, I got the error message as the web.config doest not have the <trust> node.