Click here to Skip to main content
13,089,209 members (64,851 online)
Click here to Skip to main content
Add your own
alternative version


5 bookmarked
Posted 1 Apr 2013

PowerShell - Wait for Retract / Uninstall before Remove

, 1 Apr 2013
Rate this:
Please Sign up or sign in to vote.
Wait for Retract / Uninstall before Remove.
This is a very small tip for SharePoint developers. Basically people hate PowerShell before knowing it. But you will love it once you start using it. Sooooo Comfortable.

So, as a part of PowerShell commands, we have UnInstall-SPSolution command for retracting a SharePoint solution and Remove-SPSolution command to remove the solution from any scope.

When you execute these commands in a sequence in a script (.ps1) file, Usually we end up with an error saying
 Remove-SPSolution : The solution "XXXX.wsp" has been deployed in the farm. Please retract the deployment before removing the solution.You can also use the
-override parameter to forcibly remove the solution, but you will not be able to retract the solution deployment.

So we need to wait until the retraction / uninstallation is completed, to remove solution. But how we can wait in Automated scripts (.ps1) files?

Below is the way to wait . . .

I have three SharePoint solutions deployed to farm and I want a single script file which can uninstall and remove the solutions. Look at below PowerShell script.
add-pssnapin "microsoft.sharepoint.powershell"
Set-ExecutionPolicy unrestricted
echo " "
echo "Uninstalling Project1"
<yellow>$sln = get-spsolution -identity SharepointProject1.wsp </yellow>
uninstall-spsolution -identity SharepointProject1.wsp -confirm:$false
echo "Started solution retraction..." 
<yellow>while($sln.JobExists) { </yellow>
<yellow>echo " > Uninstall in progress..."</yellow>
<yellow>start-sleep -s 10 </yellow>
remove-spsolution -identity SharepointProject1.wsp -confirm:$false
echo "Removed Project1"
echo "  "
echo "Uninstalling Project2"
<yellow>$sln = get-spsolution -identity SharepointProject2.wsp </yellow>
uninstall-spsolution -identity SharepointProject2.wsp -confirm:$false
echo "Started solution retraction..." 
<yellow>while($sln.JobExists) { </yellow>
<yellow>echo " > Uninstall in progress..." </yellow>
<yellow>start-sleep -s 10</yellow>
remove-spsolution -identity SharepointProject2.wsp -confirm:$false
echo "Removed Project2"
echo "  "
echo "Uninstalling Project3"
<yellow>$sln = get-spsolution -identity SharepointProject3.wsp</yellow>
uninstall-spsolution -identity SharepointProject3.wsp -confirm:$false
echo "Started solution retraction..." 
<yellow>while($sln.JobExists) { </yellow>
<yellow>echo " > Uninstall in progress..." </yellow>
<yellow>start-sleep -s 10 </yellow>
remove-spsolution -identity SharepointProject3.wsp -confirm:$false
echo "Removed Project3"
Now here is the output:

Task accomplished!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...

Comments and Discussions

QuestionRunning into error with above scripts Pin
TechBang21-Oct-15 2:49
memberTechBang21-Oct-15 2:49 
AnswerRe: Running into error with above scripts Pin
TechBang21-Oct-15 2:57
memberTechBang21-Oct-15 2:57 
GeneralGreat info! Worked for me Pin
Member 350908028-Aug-13 11:14
memberMember 350908028-Aug-13 11:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 1 Apr 2013
Article Copyright 2013 by PratapReddyP
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid