关于Chocolatey中Install-ChocolateyPackage未明文档softwareName参数的问询
softwareName Parameter in Install-ChocolateyPackage Great question! The softwareName parameter in Chocolatey's Install-ChocolateyPackage is one of those underdocumented workhorses that makes package management smoother once you grasp its purpose. Let's break down exactly what it does and how to use it correctly.
What is softwareName?
At its core, this parameter is Chocolatey's way of identifying existing installations of your target software in the Windows registry (specifically the Uninstall keys). It’s critical for reliable uninstall, upgrade, and duplicate-install prevention logic—even though the official docs don’t dive into its nuanced rules.
Core Use Cases & Rules
1. Wildcard Matching (For Versioned or Variable Display Names)
Many applications have a DisplayName in the registry that includes a version number (e.g., Visual Studio Code 1.86.0). In these cases, using a wildcard (*) in softwareName lets Chocolatey match any version of the software:
- How it works: Chocolatey scans the system’s uninstall registry entries for any entry whose
DisplayNamecontains the text between wildcards. - Rule of thumb: Use wildcards when the software’s display name changes with each version, or when you need to match multiple variants of the same app (e.g., 32-bit vs 64-bit if their display names are nearly identical).
2. Exact Name Matching (For Fixed Display Names)
If the target software has a static, version-agnostic DisplayName (like 7-Zip or Notepad++), you can pass the exact display name as softwareName:
- How it works: Chocolatey looks for an exact match (case-insensitive) in the registry’s
DisplayNamefield. - Rule of thumb: Only use exact names when you’re certain the display name never changes between versions—this ensures precise targeting of the correct uninstall entry.
Practical Examples
Example 1: Wildcard Usage for Versioned Apps
Install-ChocolateyPackage ` -PackageName "vscode" ` -SoftwareName "*Visual Studio Code*" ` -Url "https://code.visualstudio.com/sha/download?build=stable&os=win32-x64-user" ` -UninstallArguments "/silent /norestart"
Here, *Visual Studio Code* matches any version of VS Code, regardless of the version number appended to its registry display name.
Example 2: Exact Name Usage for Fixed Display Name Apps
Install-ChocolateyPackage ` -PackageName "7zip" ` -SoftwareName "7-Zip" ` -Url "https://www.7-zip.org/a/7z2301-x64.msi" ` -UninstallArguments "/qn /norestart"
7-Zip’s registry DisplayName is always 7-Zip, so passing the exact name ensures Chocolatey targets the correct uninstall entry every time.
Common Pitfalls to Avoid
- Overly broad wildcards: Using something like
*Code*might accidentally match unrelated apps (e.g.,JetBrains Riderif it includes "Code" in its display name). Keep wildcards as specific as possible. - Incorrect display names: Always verify the exact
DisplayNamein the registry (checkHKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\UninstallorHKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstallfor user-specific installs) before settingsoftwareName. - Ignoring its role beyond uninstall:
softwareNamedoesn’t just handle uninstalls—it also helps Chocolatey detect existing installations to skip redundant installs or trigger upgrade logic instead of a fresh install.
内容的提问来源于stack exchange,提问作者antonio

