April 14, 2010 04:15 PM

Managing Solution Packages Using PowerShell

SharePoint Pro
InstantDoc ID #125074
Rating: (14)

Windows PowerShell comes in handy for installing as well as removing a solution from a SharePoint farm. It’s relatively easy to do and can save you time. Let’s walk through how it’s done.

Installation

You can install a solution package by loading Microsoft.SharePoint.Powershell snap-in and then calling the Add-SPSolution cmdlet passing the –LiteralPath parameter using a value that contains the physical path to the solution package file.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction
  "SilentlyContinue"
Add-SPSolution -LiteralPath "C:\Solutions\Wingtip
  DevProject1.wsp"

Once you have added the solution package using the Add-SPSolution cmdlet, you can deploy it using the Install-SPSolution cmdlet. Note that once the solution package has been added, you refer to it using the –Identity parameter whose value should be the name of the solution package file without any path.

Add-PSSnapin Microsoft.SharePoint.PowerShell
  -ErrorAction"SilentlyContinue"
Add-SPSolution -LiteralPath "C:\Solutions\Wingtip
  DevProject1.wsp"
Install-SPSolution -Identity "WingtipDevProject1.wsp"
  -Local -GACDeployment

You should also observe that the call to Install-SPSolution includes two additional parameters which are –Local and –GACDeployment. The –Local parameter tells SharePoint Foundation that it only needs to worry about deploying the solution package on the local server which can speed things up when developing on a single-server farm. The other parameter –GACDeployment is required whenever you are deploying a solution package which installs an assembly in the GAC.

Retracting and Removing a Solution

If you want to remove a solution package from a farm after it has been deployed you must retract it and then remove it. The act of retracting reverses the act of deployment. For example, retracting a solution will force SharePoint Foundation to delete all the files it copied during deployment as well as uninstalling features and deleting assemblies from the GAC. Once you have retracted a solution you can then remove it which deletes the solution package file from the configuration database.

You can retract a solution package using the Uninstall-SPSolution cmdlet. When calling Uninstall-SPSolution you should pass the -Identity parameter and the -Local parameter in the same manner as when calling Install-SPSolution. You should also pass the –Confirm parameter with a value of $false because failing to do so can cause the cmdlet to prompt the user. This can cause problems because it has the effect of freezing Visual Studio when you execute a script that prompts the user for a response.

Add-PSSnapin Microsoft.SharePoint.PowerShell
  -ErrorAction "SilentlyContinue"
$SolutionPackageName = "WingtipDevProject1.wsp"
Uninstall-SPSolution -Identity $SolutionPackageName
  -Local -Confirm:$false

Once you have retracted the solution using the Uninstall-SPSolution cmdlet, you can then remove it by calling Remove-SPSolution which instructs SharePoint Foundation to delete the solution package file from the configuration database.

Add-PSSnapin Microsoft.SharePoint.PowerShell
  -ErrorAction "SilentlyContinue"
$SolutionPackageName = "WingtipDevProject1.wsp"
Uninstall-SPSolution -Identity $SolutionPackageName
  -Local -Confirm:$false
Remove-SPSolution -Identity $SolutionPackageName
  -Confirm:$false

Of course, these calls to Uninstall-SPSolution and Remove-SPSolution will fail if the solution package is not currently installed and deployed. Therefore, it makes sense to add a call to Get-SPSolution and conditional logic to determine whether the solution package is currently installed and deployed before attempting to retract it or remove it.

Add-PSSnapin Microsoft.SharePoint.PowerShell
  -ErrorAction "SilentlyContinue"
$SolutionPackageName = "WingtipDevProject1.wsp"
$solution = Get-SPSolution | where-object \{$_.Name
  -eq $SolutionPackageName\}
# check to see if solution package has been installed
if ($solution -ne $null) \{
# check to see if solution package is currently deployed
if($solution.Deployed -eq $true)\{
Uninstall-SPSolution -Identity $SolutionPackageName
  -Local -Confirm:$false
\}
\}

Related Content:

ARTICLE TOOLS

   
Comments
  • Rask
    2 years ago
    Aug 11, 2010

    if you need the status of the timer job that installs/uninstalls solution, check $solution.JobExists

    while ( $solution.JobExists )
    {
    write-host "."
    sleep 1
    }
    write-host "solution deployed"

    This is especially handy when doing uninstall/remove/add/install

You must log on before posting a comment.

Are you a new visitor? Register Here
   
   

Dan Holme's Viewpoint on SharePoint Blog

Office 365 Plan for Pain

With cloud services, even Office 365, what you don’t know about your cloud service can hurt you,...

SharePoint News and Products

Let SharePoint Be SharePoint: Making Social Collaboration Secure

Hesitant about unleashing SharePoint's social features? SharePoint security vendors aim to help....

Dan Holme's Viewpoint on SharePoint Blog

Microsoft SkyDrive Updates in the News

Microsoft's cloud storage, sharing, and collaboration platform for Windows Live is updated,...