none
Uninstall SQL 2008 R2 with Powershell

    Question

  • Morning Guys and Gals

    I need some help writing a Powershell script that uninstalls SQL 2008 R2 completely on a remote computer. When I did a search the closest answer I came up with was on this site (http://sqlblog.com/blogs/rob_farley/archive/2011/05/23/powershell-script-to-help-uninstall-sql-server-2008-r2-evaluation-edition.aspx) script is below:

    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

    I can run the powershell portion of this correctly but when I tried to run the uninstallR2.bat file, nothing happens. A cmd prompt window opens and then closes no error no flashy lights or anything. I opened the uninstallR2.bat file in Notepad and one of the lines is as follows:

    rem SQL Server 2008 R2 Common Files
    msiexec /x "{234F6B0D-10AE-4BB7-B2F3-E48D4861952D}"

    Looking up msiexec and its switch. It looks right. After kind of putting this code to the side and looking for another one I found this one:

    $machineName = Read-host "Enter a Machine Name:"
    #$userName = Read-Host "Enter your admin account:"
    Get-WmiObject -Class Win32_Product -ComputerName $machineName -impersonation Impersonate | Out-GridView
    $appName = Read-host "Enter an Application Name:"
    $app = Get-WmiObject -Class Win32_Product -computerName $machineName -impersonation Impersonate | Where-Object {$_.Name -Match $appName}
    $app.Uninstall()

    The problem with the code above, is that it queries the remote machine and asks the user for the program names. While this can be kind of automated for small programs Adobe PDF Reader and Flash, for a program like SQL 2k8 R2 which has multiple installs I can't see this one working. Can anyone help me get either one of these two scripts working????

    Any help would be greatly appreciated.

    Thursday, April 5, 2012 1:36 PM

Answers

  • I suppose not. I tried both methods and they do work. 

    What I ended up doing is doing it manually, because I was not 100% percent confident in the script I wrote. Someday I will be able to figure it out though.

    • Marked as answer by M Schroeder Friday, April 13, 2012 7:18 PM
    Friday, April 13, 2012 7:18 PM

All replies

  • rem SQL Server 2008 R2 Common Files
    msiexec /x "{234F6B0D-10AE-4BB7-B2F3-E48D4861952D}"

    Hi,

    Notice that this command is only uninstalling a package called "SQL Server 2008 R2 Common Files". I would guess that this isn't the main package.

    Try this PowerShell script instead:

    Auditing 32-Bit and 64-Bit Applications with PowerShell

    Then you can use a command like this:

    get-installedapp -appname "*SQL*R2*" -matchall |
      select-object -expandproperty AppID |
      foreach-object { "msiexec /x ""$_""" }
    

    This command selects all application names that match the pattern, and selects the AppID for each one (the "{xxxxx}" GUID), and then executes the msiexec uninstall command for each. The " characters around the msiexec command are for testing. To actually remove the package, you would need to use

    ... | foreach-object { msiexec /x "$_" }

    instead.

    To see what applications the above command would remove, run this first:

    get-installedapp -appname "*SQL*R2*" -matchall  | select-object AppName,AppID

    HTH,

    Bill

    Thursday, April 5, 2012 2:02 PM
    Moderator
  • Cool thanks. I will give that a shot in a little bit and will let you know how that works out.
    Thursday, April 5, 2012 2:24 PM
  • Have another question for that script you mentioned how do i send a computername to it? I want to run this script for a remote computer. Is there a way to import your script into Powershell so I don't have to call it everytime?
    Thursday, April 5, 2012 4:07 PM
  • figured out how to run the script against a remote computer.

    The next problem is that it tries to uninstall all of the SQL R2 items all at the exact same time. Is it possible to stagger them, one after the other?

    Thursday, April 5, 2012 4:16 PM
  • Hi,

    You could do what the blog writer did earlier - redirect the uninstall commands to a separate script file, and then run that. For example:

    get-installedapp -appname "*SQL*R2*" -matchall |
      select-object -expandproperty AppID |
      foreach-object { "msiexec /x ""$_""" } |
      out-file uninstall.cmd -encoding ASCII

    Bill

    Thursday, April 5, 2012 4:32 PM
    Moderator
  • You could also use the Windows System Administration tool linked in my signature.  It can uninstall programs remotely and many other things.

    Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP

    Engineering Efficiency
    @Rich_Prescott
    Windows System Administration tool
    AD User Creation tool

    Friday, April 6, 2012 2:53 AM
    Moderator
  • My knowledge of Powershell is new...or rather limited right now. So I would prefer not to open up another file to run something. Is there a way that I can uninstall SQL 2k8 R2 completely. Not needing another script run afterwards.

    That's why I was kind of thinking that the below code would do that for me, but with something like SQL which installs a ton of different things with it. It's hard to know what to remove. I guess I could hard code each program that needs to be removed, it would be a lengthier script but should still work. What do you think?

    $machineName = Read-host "Enter a Machine Name:" 
    #$userName = Read-Host "Enter your admin account:" 
    Get-WmiObject -Class Win32_Product -ComputerName $machineName -impersonation Impersonate | Out-GridView
    $appName = Read-host "Enter an Application Name:"
    $app = Get-WmiObject -Class Win32_Product -computerName $machineName -impersonation Impersonate | Where-Object {$_.Name -Match $appName}
    $app.Uninstall()

    Tuesday, April 10, 2012 1:06 PM
  • My knowledge of Powershell is new...or rather limited right now. So I would prefer not to open up another file to run something. Is there a way that I can uninstall SQL 2k8 R2 completely. Not needing another script run afterwards.

    That's why I was kind of thinking that the below code would do that for me, but with something like SQL which installs a ton of different things with it. It's hard to know what to remove. I guess I could hard code each program that needs to be removed, it would be a lengthier script but should still work. What do you think?

    $machineName = Read-host "Enter a Machine Name:" 
    #$userName = Read-Host "Enter your admin account:" 
    Get-WmiObject -Class Win32_Product -ComputerName $machineName -impersonation Impersonate | Out-GridView
    $appName = Read-host "Enter an Application Name:"
    $app = Get-WmiObject -Class Win32_Product -computerName $machineName -impersonation Impersonate | Where-Object {$_.Name -Match $appName}
    $app.Uninstall()

    Tuesday, April 10, 2012 1:06 PM
  • Is there a way that I can uninstall SQL 2k8 R2 completely. Not needing another script run afterwards.

    Why does it matter, if it solves the problem?

    Bill

    Friday, April 13, 2012 5:07 PM
    Moderator
  • I suppose not. I tried both methods and they do work. 

    What I ended up doing is doing it manually, because I was not 100% percent confident in the script I wrote. Someday I will be able to figure it out though.

    • Marked as answer by M Schroeder Friday, April 13, 2012 7:18 PM
    Friday, April 13, 2012 7:18 PM