1

I tried to uninstall Microsoft Visual C++ 2022 X64 in a powershell script with this code :

$Name = "Microsoft Visual C++ 2022 X64"
$nameLikePackage = "name like '%%$Name%%'"
$uninstallCommand = "wmic product where ""$nameLikePackage"" call uninstall /nointeractive"
Write-Host "Uninstalling $Name ..."
Invoke-Expression $uninstallCommand

After execution this message appear :

instance of __PARAMETERS
{
    ReturnValue = 0;
};

Of course I execute the powershell script as an administrator.

And when I check (with wmic in a cmd or in applications and funtionnalities in windows) if Microsoft Visual C++ 2022 is correctly uninstall, I see that nothing change and it's still install.

After seeing your comments I tried two methods.

The first one is with Uninstall-Package, here is the code :

Try
{
    $Name = "Microsoft Visual C++ 2022 X64"
    $wmiObjectToUnin = Get-WmiObject -Class Win32_Product -Filter "Name LIKE '%$Name%'"
    foreach ($component in $wmiObjectToUnin)
    {
        $componentName = $component.Name
        Write-Host "Uninstalling $componentName ..."
        Uninstall-Package -Name $componentName
    }        
}
Catch
{
    WriteHost "Package Microsoft Visual C++ X64 not install"
}

The second one is with the uninstall function of a wmi object, here is the code :

Try
{
    $Name = "Microsoft Visual C++ 2022 X86"
    $wmiObjectToUnin = Get-WmiObject -Class Win32_Product -Filter "Name LIKE '%$Name%'"
    foreach ($component in $wmiObjectToUnin)
    {
        Write-Host "Uninstalling $component.Name ..."
        $component.Uninstall()
    }
}
Catch
{
    WriteHost "Package Microsoft Visual C++ X86 not install"
}

But in both cases, nothing is uninstalled, even though the powershell window indicates that the command was executed successfully.

  • 1
    Perhaps this can be done with PowerShell. WMI is deprecated. WMI is not available in PowerShell 6+. Use CIM. Find the `Name` of your product with `(Get-CimInstance -ClassName CIM_Product).Name`. Then see https://www.urtech.ca/2019/09/solved-command-line-to-uninstall-software-exes-or-msis/ – lit Mar 10 '23 at 12:16
  • A possible alternative is to use `Uninstall-Package` - see [this answer](https://stackoverflow.com/a/68383173/45375). – mklement0 Mar 10 '23 at 15:02
  • As an aside: [`Invoke-Expression` (`iex`) should generally be avoided](https://stackoverflow.com/a/51252636/45375); definitely [don't use it to invoke an external program or PowerShell script / command](https://stackoverflow.com/a/57966347/45375). – mklement0 Mar 10 '23 at 15:04
  • @lit I tried with CIM too (like wmic) and I have the same problem. PS : I edited the post and add new informations – Andréa Madrid Mar 14 '23 at 14:52
  • @mklement0 I tried with ```Uninstall-Package``` but nothing changed. PS : I edited the post and add new informations – Andréa Madrid Mar 14 '23 at 14:53
  • @AndréaMadrid, I don't have a solution, but as an aside: you don't need WMI with `Uninstall-Package`; something like `Get-Package '*Microsoft Visual C++ 2022 X64*' | Uninstall-Package -WhatIf` should do (I've added `-WhatIf` so you can _preview_ the operation). – mklement0 Mar 14 '23 at 14:58
  • 1
    @mklement0 I didn't know that, thanks, I'll try. By the way, since I posted this question, I've been looking for other solutions to uninstall this package with a powershell script and I found Powershell App Deployment Toolkit. If you have any knowledge about this, I posted another question, here is the [link](https://stackoverflow.com/questions/75732315/how-to-uninstall-visual-c-2022-with-powershell-app-deployment-toolkit) – Andréa Madrid Mar 14 '23 at 15:13

1 Answers1

0

After many hours of research, I found out that the problem was the user rights of the company where I work (because I don't have 100% administrator rights).

If you are like me, you can try to use PSAppDeployToolkit Command but if it doesn't work too there is this command :

Try
{
    ## Check if the package exist
    $wmiObjectToUnin = Get-WmiObject -Class Win32_Product -Filter "Name LIKE 'Microsoft Visual C++ 2022 X64%'"

    if($wmiObjectToUnin -eq $null)
    {
        throw "Microsoft Visual C++ 2022 X64 not install"
    }

    else
    {
        Show-InstallationProgress -StatusMessage "Uninstalling Microsoft Visual C++ 2022 X64 ..."
        $vcRedistPath = "{your-path}\VC_redist.x64.exe"
        $params = "/uninstall /quiet /norestart"
        Start-Process -FilePath $vcRedistPath -ArgumentList $params -Wait
    }
}

Catch
{
    Show-InstallationProgress "$_"
    timeout 3
}

Try
{
    ## Check if the package exist
    $wmiObjectToUnin = Get-WmiObject -Class Win32_Product -Filter "Name LIKE 'Microsoft Visual C++ 2022 X86%'"

    if($wmiObjectToUnin -eq $null)
    {
        throw "Microsoft Visual C++ 2022 X86 not install"
    }

    else
    {
        Show-InstallationProgress -StatusMessage "Uninstalling Microsoft Visual C++ 2022 X86 ..."
        $vcRedistPath = "{your-path}\VC_redist.x86.exe"
        $params = "/uninstall /quiet /norestart"
        Start-Process -FilePath $vcRedistPath -ArgumentList $params -Wait
    }
    
}

Catch
{
    Show-InstallationProgress "$_"
    timeout 3
}

{your-path} is the path where the file VC_redist.x64.exe (for an x64 version) or VS_redist.x86.exe (for an x86 version) is located.

These files are the installation files for Microsoft Visual C++ 2022, you can download them here.

  • 1
    Good to hear you found a solution, but I'm puzzled: If your problem is that you lack the administrative privileges required to run the uninstaller, why does running `VC_redist.x64.exe` work? Does the latter _not_ require admin privileges? – mklement0 Mar 15 '23 at 13:58
  • 1
    @mklement0 To be honest, the only information I have is that I have some administrative privileges but I don't have full rights. So the difference is that ``VC_redist.x64.exe`` has all the path details of the files that make up Microsoft Visual C++ 2022 x64 whereas when I run the commands to uninstall, it calls other commands (to access all the necessary paths) that need full administrative privileges. – Andréa Madrid Mar 15 '23 at 14:29