Share via

Remotely and Silently uninstalling a couple different office products fail with an error code 1639 in powershell

Anonymous
2023-06-07T18:47:40+00:00

So as the title says I am trying to put together a powershell script that allows me to enter in the name of a computer, then enter in the name of some software and then it will uninstall that software. Specifically right now I am trying to get it to uninstall Microsoft Visio Standard 2013 and Microsoft Project Standard 2013. The script does not appear to work with any software I specify, despite it stating the uninstall completes. However with the two previously mentioned programs it fails with an error code 1639 and I am not sure why. The script should search for any specifically named uninstall methods, in this case I have three, one for chrome, one for adobe, and one for office in general and if none of those match it moves on to try two 'generic' uninstall methods. I guess basically I need to know what special parameters Microsoft Visio Standard 2013 and Microsoft Project Standard 2013. If possible I'd like to know if there is something similar with Microsoft Project Standard 2016. With Chrome it just straight up says it can find the file,

Error Log when trying to uninstall project standard:

Microsoft Project Standard 2013: Uninstallation failed with exit code: 1639

Microsoft Project Standard 2013: Uninstallation failed with exit code: 1639

Here is a copy of the script:

# Prompt for administrator credentials 

$credentials = Get-Credential -Message "Enter Administrator Credentials"

# Enter domain computer name

$computerName = Read-Host "Enter the domain computer name"

# Enter the name of the software to be uninstalled

$softwareName = Read-Host "Enter the name of the software to uninstall"

# Specify the log file path

$logFilePath = "C:\Users\***\Documents\uninstall.log"

# Function to uninstall software by product code

function Uninstall-SoftwareByProductCode {

    param (

        [Parameter(Mandatory = $true)]

        [string]$ProductCode

    )

    $arguments = "/x $ProductCode /qn /norestart"

    Write-Host "Uninstalling software with product code: $ProductCode"

    try {

        $process = Start-Process -FilePath "msiexec.exe" -ArgumentList $arguments -NoNewWindow -Wait -PassThru

        if ($process.ExitCode -eq 0) {

            Write-Host "Software successfully uninstalled."

        } else {

            throw "Uninstallation failed with exit code: $($process.ExitCode)"

        }

    } catch {

        Write-Host "Error: $_"

        $errorMessage = "${softwareName}: $_"

        $errorMessages += $errorMessage

    }

}

# Function to find product code by software name

function Get-ProductCodeByName {

    param (

        [Parameter(Mandatory = $true)]

        [string]$SoftwareName

    )

    $key = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"

    $subkeys = Get-ItemProperty -Path $key | Where-Object { $_.DisplayName -like "*$SoftwareName*" }

    if ($subkeys) {

        $subkey = $subkeys | Select-Object -First 1

        return $subkey.PSChildName

    } else {

        return $null

    }

}

# Initialize error messages variable

$errorMessages = @()

# Method 1: Adobe Acrobat

if ($softwareName -eq "Adobe Acrobat") {

    $productCode = Get-ProductCodeByName -SoftwareName $softwareName

    if ($productCode) {

        Uninstall-SoftwareByProductCode -ProductCode $productCode

        exit

    }

}

# Method 2: Microsoft Office

if ($softwareName -eq "Microsoft Office") {

    $productCode = Get-ProductCodeByName -SoftwareName $softwareName

    if ($productCode) {

        Uninstall-SoftwareByProductCode -ProductCode $productCode

        exit

    }

}

# Method 3: Google Chrome

if ($softwareName -eq "Google Chrome") {

    Write-Host "Uninstalling Google Chrome"

    try {

        $process = Start-Process -FilePath "C:\Program Files\Google\Chrome\Application\uninstall.exe" -ArgumentList "/S" -Credential $credentials -NoNewWindow -Wait -PassThru

        if ($process.ExitCode -eq 0) {

            Write-Host "Google Chrome successfully uninstalled."

        } else {

            throw "Uninstallation failed with exit code: $($process.ExitCode)"

        }

    } catch {

        Write-Host "Error: $_"

        $errorMessage = "${softwareName}: $_"

        $errorMessages += $errorMessage

    }

    exit

}

# Method 4: Custom Uninstall Methods

# Uncomment and add your custom uninstall methods here

# Method 5: Common MSI Silent Uninstall

Write-Host "Attempting common MSI silent uninstall method"

$arguments = "/x {product code goes here} /qn /norestart"

try {

    $process = Start-Process -FilePath "msiexec.exe" -ArgumentList $arguments -NoNewWindow -Wait -PassThru

    if ($process.ExitCode -eq 0) {

        Write-Host "Software successfully uninstalled using common MSI method."

        exit

    } else {

        throw "Uninstallation failed with exit code: $($process.ExitCode)"

    }

} catch {

    Write-Host "Error: $_"

    $errorMessage = "${softwareName}: $_"

    $errorMessages += $errorMessage

}

# Method 6: Common Product Code Silent Uninstall

Write-Host "Attempting common product code silent uninstall method"

$arguments = "/x {product code goes here} /qn /norestart"

try {

    $process = Start-Process -FilePath "msiexec.exe" -ArgumentList $arguments -NoNewWindow -Wait -PassThru

    if ($process.ExitCode -eq 0) {

        Write-Host "Software successfully uninstalled using common product code method."

        exit

    } else {

        throw "Uninstallation failed with exit code: $($process.ExitCode)"

    }

} catch {

    Write-Host "Error: $_"

    $errorMessage = "${softwareName}: $_"

    $errorMessages += $errorMessage

}

# Generate error log if any errors occurred

if ($errorMessages) {

    $errorLogPath = "C:\Users\user.name\Documents\uninstall.log"

    Write-Host "Generating error log at $errorLogPath"

    $errorMessages | Out-File -FilePath $errorLogPath -Encoding UTF8

} else {

    Write-Host "All uninstallation methods failed. No errors to log."

}

Microsoft 365 and Office | Project | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2023-06-08T09:02:20+00:00

Dear BrettPatITT,

It seems that you are in a local AD environment. Sorry for that our category may have limited resources on coping with custom PowerShell script related issues and questions.

I kindly suggest you post a new thread on our specific support channel PowerShell - Microsoft Q&A with the script you posted here so that the engineers there can look into the script and help you find a solution.

Thanks for your understanding and hope that the information above helps you!

Sincerely

Cliff | Microsoft Community Moderator

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful