Search code examples
powershellvisual-c++wmic

How to uninstall Visual C++ 2022 with wmic in a powershell script


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.


Solution

  • 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.