If you haven’t been caught by a client (or even yourself) installing SQL 2008 R2 Evaluation Edition and then letting it expire, you might not realise the pain that this causes. To say it’s frustrating is a serious understatement.
Fellow SQL MVP and SQLBlog blogger Aaron Bertrand (@aaronbertrand) has an excellent post on the matter, which he put together back in October 2010. I had cause to use it recently, but got somewhat put off by searching through the registry.
So I put a line of PowerShell together (which I’ve split across 5 for easier reading):
1 2 3 4 5 |
Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | select @{Name='Guid';Expression={$_.PSChildName}}, @{Name='Disp';Expression={($_.GetValue("DisplayName"))}} | where-object {$_.Disp -ilike "*SQL*R2*"} | where-object {$_.Guid -like '{*'} | % {"rem " + $_.Disp; 'msiexec /x "' + $_.Guid + '"'; ''} > uninstallR2.bat |
If you run this, it will produce the file that Aaron recommended. You can look down it for anything you don’t want to get rid of, and maybe reorder the odd thing, and then run it. Then you should be able to install Developer Edition, and breathe much easier.
The script is quite straight forward, it just lists everything in the uninstall bit of the registry, gets the DisplayName values out, filters them, and outputs the results in a few lines for the batch file. Nothing amazingly complicated, but useful for getting through the list quickly.
(Big thanks to Aaron for working out what was required for the uninstall, and Aaron Nelson (@sqlvariant) for answering a quick question when I was putting the final touches on the script)
This Post Has 21 Comments
What I have to change in this script to uninstall SQL Server 2008 evaluation after I have installed SQL Server 2008 R2?
Hi Frank,
Tweak the bit that says *SQL*R2*. You could even just put * and then go through the resultant batch file to look for the things you want to keep. But maybe keep the *SQL* bit and then look for the stuff you want to get rid of.
Rob
I created the uninstallR2.bat file using the PowerShell script provided above.
Unfortunately the bat file does not run.
I get a message telling me that
"_R" is not recognized as an internal or external command, operable program or batch file"
Note the under score is actually a square symbol.
I am wondering if the bat file name or location is my issue?
I looked at the services running and found MsDtsServer100 running so I stopped it. Maybe there are other SqlServer services to stop?
I also ran the bat file as administrator.
Any assistance will be appreciated.
Hi Theo – what’s in your .bat file? Does it contain any strange characters?
Hi Rob, to the eye nothing noticeable.
As an experiment I REM all the msiexec and the error still occurs.
I created a bat file with a couple of REM lines and it worked ok.
The UninstallR2.bat result I got running your powershell script gave me 23 msiexec lines comparing to Aaron Bertrand’s seems excessive?
Given that I have never used Powershell scripting before I became suspicious that maybe the file did not get created with the correct properties.
So I copied the contents into another file I created using note pad and it is now running.
Not sure what the issue was, maybe Powershell needs configuring?
(just started typing again)
Yes – I suppose it’s possible that somewhere along the way there’s a strange character getting inserted instead of a space. I’m pleased you’ve got it working.
Thanks for your help.
The bat file generated 23 uninstall commands, working through the registry would have been a time consuming trial and error exercise.
I ran the bat file 3 times, on the 3rd run it didn’t find anything to uninstall.
However, under Start-All Programs I still see some Sql Server 2008 menu options.
I can still run the Installation Center, documentation and tutorials and Visual Studio.
I will try to install the developer edition tonight and see if that works.
You might see some SQL 2008 entries still, because you were only looking for R2 stuff. If you tweak the script a little, you can probably find the list of SQL 2008 things too.
Thanks the only item I couldn’t remove was the Sql Server 2008 R2 documentation.
I will try to install the developer edition now and see if all goes well.
The odd characters are unicode. If you save the batch file using notepad, save as, and specify ANSI rather than Unicode, it should work fine
I just used this to give my infrastructure guys an uninstall script for all my various bits of sql server lying around. This has saved me so much time and if they say it doesn’t work, then I’ll chuck it into notepad and save as ANSI. I’ve been looking for something like this for ages. Great work!
Thanks, you ‘ve saved lot of my time 🙂 Nice job !
Hi Rob,
I am trying to use your script to help me remove a SQL Server 2008 R2 installation but when I run it I get this message below:
Get-ChildItem : A parameter cannot be found that matches parameter name ‘System.Object[]’.
Should I be able to just run your script exactly the way you have written it ?
Thanks.
It runs just fine on my machine as is.
Try changing directory one level at a time into HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall to make sure you can. Then slowly add the rest of the script to see what fails.
Could be you don’t have enough privilege? You could try making sure you have admin rights escalated in your PowerShell window (ie, Run As Administrator).
Brilliant, thanks! Service Pack 1 was not picked up by the script, and I am unable to uninstall it or a hot fix I had installed. Be sure to remove those first before uninstalling sql server.
To What THeo was facing with some strange characters. If you save the bat file in ASCII format you wont get that strange character.
Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall |
select @{Name=’Guid’;Expression={$_.PSChildName}}, @{Name=’Disp’;Expression={($_.GetValue("DisplayName"))}} |
where-object {$_.Disp -ilike "*SQL*R2*"} |
where-object {$_.Guid -like ‘{*’} |
% {"rem " + $_.Disp; ‘msiexec /x "’ + $_.Guid + ‘"’; ”} | Out-File .\uninstallR2.bat -Encoding ascii
Hi guy!
You are Great! 🙂 Thank You very much for script, helped a lot.
p.s.: I\d recomment to add "/quiet" on the end of msiexec command – it will avoid confirmations and dialogs
Best regards,
Gennady
No worries. Glad it helped.
Hello i’ve the solution :
you need to encode the result file (uninstallR2.bat) in UTF8 without Bom
following a powershell code :
$Path ="D:\DBA\uninstallR2.bat"
$MyFile = Get-Content $Path
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
[System.IO.File]::WriteAllLines($Path, $MyFile, $Utf8NoBomEncoding)
And you can execute :
## Execute command file
$Path ="D:\DBA\uninstallR2.bat"
pushd $Path
.\uninstallR2.bat
popd
Enjoy
Yes, Eric. If you make a Unicode file, it won’t work. It must be ASCII / UTF8.