powershellpackagewildcarddism

Using wildcard in a batch file with dism function


How would I go about using a wild card for this:

dism /online /remove-package /PackageName:Package_for_KB2952664~31bf3856ad364e35~amd64~~6.1.16.0

I have tried this:

dism /online /remove-package /PackageName:Package_for_KB2952664~31bf3856ad364e35~amd64~~6.*.*.*

Solution

  • You cannot use wildcards in the /PackageName argument. You can retrieve the full list of packages, search the resulting set for the packages you want, then run the remove command for each result.

    First, get all the packages:

    $packages = dism /online /Get-Packages
    

    Next, find only the packages that you care about with a regular expression:

    $matched_packages = $packages | Select-String "Package_for_KB2952664~31bf3856ad364e35~amd64~~.*$"
    

    In the above command, the regex Package_for_KB2952664~31bf3856ad364e35~amd64~~.*$ can be changed to a more appropriate regex to search for, if needed.

    Next, filter down to just the values from the matches to make it easier to iterate over:

    $matched_values = $matched_packages | ForEach-Object { $_.Matches[0].Value }
    

    Finally, run the remove-package command on each match:

    $matched_values | % {dism /online /remove-package /PackageName:$_}
    

    You can make it a one-liner if you prefer:

    dism /online /Get-Packages | Select-String "Package_for_KB2952664~31bf3856ad364e35~amd64~~.*$" | ForEach-Object { $_.Matches[0].Value } | % {dism /online /remove-package /PackageName:$_}
    

    There may be a simpler way of constructing these commands (my PowerShell is a little rusty) but the above examples will execute the remove-package command for each installed version of the package specified in the regex.