PowerShell Script to deploy WSP Solution in SharePoint 2013/2016

This PowerShell SharePoint tutorial explains, how to deploy wsp in SharePoint 2013 using Powershell. It is easy to add wsp in SharePoint 2013 using PowerShell.

From SharePoint 2010 onwards Microsoft has improved the ability of PowerShell scripting more and more to make SharePoint admin life easy and smooth. In this article I would like to present one single PowerShell script to perform:

  • Deactivate feature(s) in SharePoint 2013
  • Retract the solution from a specific web application(s) in SharePoint 2013
  • Deploy the solution to a specific web application(s) in SharePoint 2013
  • Activate feature(s) in SharePoint 2013

Usually, we provide a couple of PowerShell commands to admin for deploying a SharePoint solution but my requirement is to deploy the solution through the scheduler. To accomplish that task I have created the below PowerShell script.

PowerShell Script to deploy WSP Solution in SharePoint 2013/2016

In this PowerShell script I am using sleep, do while looping to ensure that SharePoint performs the required operation.

try {
$solution = "MStechnologySolution.wsp"
$slnFlag = Get-SPSolution -Identity $solution
if ($slnFlag -ne $null) {
Write-Host "`nDisabling MTGHubFinance feature…"
do {
$ftr = Disable-SPFeature –Identity "c41791bf-c5c1-430c-8fb1-2584799d6091" –url http://intranet.mstechnology.com/ –Confirm:$false
Start-Sleep -Seconds 10
}
while ($ftr -eq $null)
Write-Host "`nMTGHubFinance feature Disabled Sucessfully"
$slnStat = Uninstall-SPSolution -Identity $solution -WebApplication http://intranet.mstechnology.com/ –Confirm:$false
if ($slnStat.Deployed -eq $null) {
Write-Host "`nUnInstalling $solution solution…"
$flgWSPSln = $false
do {
$wspID = Get-SPSolution -Identity $solution
$tmJob = Get-SPSolution -Identity $solution
if ($wspID.Deployed -eq $null) {
$flgWSPSln = $false
}
else {
if (!($wspID.Deployed -eq $False -And $tmJob.JobExists -eq – $False)) {
$flgWSPSln = $true
Start-Sleep -Seconds 10
}
else {
$flgWSPSln = $false
}
}
}
while ($flgWSPSln -eq $true)
Write-Host "`n$solution solution Uninstalled Sucessfully…"
Remove-SPSolution -Identity $solution –Confirm:$false
Start-Sleep -Seconds 2
Write-Host "`n$solution solution Removed Sucessfully…"
}
}
$slnFlag = Get-SPSolution -Identity $solution
if ($slnFlag -eq $null) {
Add-SPSolution "D:\MStechnologySolution.wsp"
Start-Sleep -Seconds 2
Write-Host "`n$solution Added Sucessfully…"
Install-SPSolution -Identity $solution -WebApplication http://intranet.mstechnology.com/ -GACDeployment
Write-Host "`nInstalling $solution …"
$flgWSPSln = $false
do {
$wspID = Get-SPSolution -Identity $solution
$tmJob = Get-SPSolution -Identity $solution
if ($wspID.Deployed -eq $True -And $tmJob.JobExists -eq – $False) {
$flgWSPSln = $true
}
else {
Start-Sleep -Seconds 10
}
}
while ($flgWSPSln -eq $false)
Write-Host "`nInstalled $solution solution Sucessfully…"
Write-Host "`nMTGHubFinance feature Enabling…."
Enable-SPFeature –Identity "c41791bf-c5c1-430c-8fb1-2584799d6091" –url http://intranet.mstechnology.com/ –PassThru
Write-Host "`nMTGHubFinance feature Enabled Sucessfully"
}
}
catch {
Write-Host "`nError:: $($_.Exception.Message)" -foregroundcolor red -BackgroundColor Yellow
}

Let us walk through the script:

  • If you see, the entire code has been wrapped inside try-catch block and we are also writing the error in output windows in case of any with the below.
Write-Host "`nError:: $($_.Exception.Message)" -foregroundcolor red -BackgroundColor Yellow
  • With the below code block, I am deactivating the feature with the ID” c41791bf-c5c1-430c-8fb1-2584799d6091″ in do while loop. If the feature is not deactivated I am forcing the PowerShell to sleep for 10 sec
do {
$ftr = Disable-SPFeature –Identity "c41791bf-c5c1-430c-8fb1-2584799d6091" –url http://intranet.mstechnology.com/ –Confirm:$false
Start-Sleep -Seconds 10
}
while ($ftr -eq $null)
  • As soon as the feature is deactivated I am first checking is the solution existed in the FARM or not
$slnFlag = Get-SPSolution -Identity $solution
  • Once the feature is deactivated, I am retracting the solution in a do while loop also checking the status of the timer job to ensure the solution retracted successfully. If the solution is not retracted I am placing the script on sleep.
$wspID = Get-SPSolution -Identity $solution
$tmJob = Get-SPSolution -Identity $solution
if ($wspID.Deployed -eq $True -And $tmJob.JobExists -eq – $False) {
$flgWSPSln = $true
}
else {
Start-Sleep -Seconds 10
}

Now that I have completed the solution retracts I am adding the solution and activating the required features by using timer jobs and do while loops.

You may like following PowerShell SharePoint tutorials:

This SharePoint PowerShell tutorial explains, how to deploy wsp farm solutions using PowerShell in SharePoint 2013/2016.

  • deploy wsp sharepoint 2013 powershell
  • deploy solution sharepoint 2013 powershell
  • how to deploy wsp in sharepoint 2013 using powershell
  • install wsp sharepoint 2013 powershell
  • add wsp sharepoint 2013 powershell
  • install spsolution
Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

  • >