Search code examples
powershellchocolatey

PowerShell, How to check for an installed Chocolatey package


For a while I was using a construct like this to check if a package was already installed before running the installer:

if (!(Test-Path "Program Files\7-Zip")) { 
    $command = "if ((choco list --local-only 7zip) -match '^0 packages') { choco install 7zip -y }"
    Start-Process "powershell.exe" -WindowStyle Hidden -Wait -ArgumentList "-sta -noprofile -executionpolicy unrestricted $command"
}

That worked well for a few years, but it now looks like they have changed the switches in Chocolatey so that choco list --local-only 7zip now generates an error:

Invalid argument --local-only. This argument has been removed from the list command and cannot be used.

What would be the most efficient way to test whether a Chocolatey package is installed or not now?


Solution

  • You are correct, with the release of Chocolatey CLI 2.0.0 (https://docs.chocolatey.org/en-us/choco/release-notes#may-31-2023) the function of the choco list command changed. So now, instead of doing:

    choco list --local-only 7zip

    you can simply do:

    choco list 7zip

    The list command now only acts on local packages, so the --local-only option is not required.