Home > Back-end >  Get-Package - PS CORE no longer provides installed software
Get-Package - PS CORE no longer provides installed software

Time:09-17

I'm moving a yaml pipeline so it uses PS Core. One of the steps is to parse currently installed software and uninstall it if it exists:


    $appsToUninstall = Get-Package -Provider Programs -IncludeWindowsInstaller -name "*$Instance*"
    if ($appsToUninstall.count -gt 0)
    {
        Get-Service | Where-Object { $_.Name -match "$Instance" } | Stop-Service
        $appsToUninstall | Uninstall-Package -Force -Verbose
        Write-Output "Uninstalled $Instance"
    }
    else
    {
        Write-Warning "Nothing to uninstall! Could not locate $Instance as an installed instance. Continuing as usual."
    }


However, it would seem that the new Get-Package - no longer provides installed software.

Doesntwork1 Doesntwork2 Works

Is there any way to use native cmdlets (not CMI/WMI [wmi is deprecated!]) to achieve that in the new PS 7 ?

CodePudding user response:

You can parse the registry to achieve this :

$Instance = "MyAppToUninstall"

# Initialize array to avoid errors on 32 bits applications addition
[array]$appsToUninstall = @()

# Get 64 bits programs
$appsToUninstall = Get-ItemProperty `
                   HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | 
                   Select DisplayName, UninstallString | 
                   Where { $_.DisplayName -like "*$Instance*" }

# Add 32 bits programs
$appsToUninstall  = Get-ItemProperty `
                    HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | 
                    Select DisplayName, UninstallString | 
                    Where { $_.DisplayName -like "*$Instance*" }

if ($appsToUninstall.count -gt 0)
{
    Get-Service | Where-Object { $_.Name -match "$Instance" } | Stop-Service
    $appsToUninstall | ForEach-Object {
        $app = $_.UninstallString.Substring(0, $_.UninstallString.IndexOf(".exe")   4)
        $arguments = $_.UninstallString.Substring($_.Uninstallstring.IndexOf(".exe")   5)
        if ($app -match "(?i)msiexec")
        {
            # if MSI package, replace /i parameter by /x to uninstall and
            # add /passive parameter to automate the uninstallation
            $arguments = "$($arguments.Replace("/I", "/x", [system.StringComparison]::InvariantCultureIgnoreCase)) /passive"
        }
        Start-Process -FilePath $app -ArgumentList $arguments
    }
    Write-Output "Uninstalled $Instance"
}
else
{
    Write-Warning "Nothing to uninstall! Could not locate $Instance as an installed instance. Continuing as usual."
}

I hope this helps :)

  • Related