1

Hey guys hopefully someone can help me. I have a powershell script that identifies old .msi(s) and deletes them from Windows/Installer folder, but I need to add some code to bypass or (skip) a certain Product ID/Source hash. Basically I need the script to skip this: {1610CB69-BE80-41B9-9B77-E346C1DA12F3} and have it not be deleted. Does anyone know how I can achieve this?

Here's my script:

<#
    This script uses VB script to identify which files need to be saved and then removes everything else.
#>

$VBSFile = @"
'' Identify which patches are registered on the system, and to which
'' products those patches are installed.
'Option Explicit
Dim msi : Set msi = CreateObject("WindowsInstaller.Installer")
'Output CSV header
WScript.Echo "The data format is ProductCode, PatchCode, PatchLocation"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("output.txt", True)
objFile.WriteLine "ProductCode, PatchCode, PatchLocation"
objFile.WriteLine ""
' Enumerate all products
Dim products : Set products = msi.Products
Dim productCode
For Each productCode in products
    ' For each product, enumerate its applied patches
    Dim patches : Set patches = msi.Patches(productCode)
    Dim patchCode
    For Each patchCode in patches
        ' Get the local patch location
        Dim location : location = msi.PatchInfo(patchCode, "LocalPackage")
        objFile.WriteLine productCode & ", " & patchCode & ", " & location

    Next
Next
WScript.Echo "Data written to output.txt, these are the registered objects and SHOULD be kept!"
"@

$VBSFile | Set-Content .\WiMsps.vbs

cscript .\WiMsps.vbs 

$savelist = Import-Csv .\output.txt

$filelocation = $savelist | select -ExpandProperty PatchLocation

#First pass to remove exact file names
dir C:\windows\Installer -file | ForEach-Object{
    $fullname = $_.FullName
    if($filelocation | Where-Object{$_ -like "*$fullname*"}){
        "Keeping $fullname"
    }
    else{
        Remove-Item $fullname -Force -Verbose
    }


}

#second pass to match product and patch codes
dir C:\windows\Installer -Directory | ForEach-Object{
    $fullname = $_.name
    if($savelist | Where-Object{$_.ProductCode -like "*$fullname*" -or $_.PatchCode -like "*$fullname*" }){
        "Keeping $fullname"
    }
    else{
        Remove-Item $_.fullname -Force -Verbose -Recurse
    }

}
pause

Basically right now it's deleting everything it can't find a source for dependency. Or at least I think it is... I got this script from someone else, so yeah basically I can't figure out what to do.

  • Remove from the array if matches a value no? Can do this for multiple values too [Array]$index = $row.Split(" ") -ne $Guid1 -ne $Guid2 – Isaac Dec 06 '19 at 15:27
  • Yeah exactly, if it matches a value. So where would I put that? [Array]$index = $row.Split(" ") -ne $Guid1 -ne $Guid2 I guess inside the (" ") I can put the product code and it will skip that? – Sean Dobbins Dec 06 '19 at 15:51
  • After you iterate through and get the list of the GUIDS into an array. then the guids you specify will be removed – Isaac Dec 06 '19 at 16:28
  • This folder must not be manually hacked like this without knowing exactly what you are doing, and even then it is dangerous. I have added some suggestions below. What is the overall goal: **`just cleanup`** or **`create disk space`** or what? – Stein Åsmul Dec 06 '19 at 17:22

1 Answers1

1

MSI Cache Folder: You must not mess around in this folder! You typically end up with products that can not be uninstalled.

  • This is a "super hidden" folder meaning it doesn't show up even when you show hidden files, you have to "show OS files" as well. That is a powerful hint of its significance and the dangers associated with changing its content.
  • All content is auto-magically managed. Some leftover files could be here, but they should be cleaned up by other mechanisms built into the OS - for example the cleanmgr.exe. More on that in the links below. The files should also be removed when the product in question is uninstalled.

Disk Space: If you are looking to clear out some disk space, perhaps try these suggestions instead: Ways to clear disk space. And there is a long version of the same list. Please visit at least the first link and locate information about cleanmgr.exe. You can also run the newer ms-settings:storagesense from the run dialog. Also described in the link.

Tools Summary: Accessible from Windows Key + Tap R:

  • cleanmgr.exe
  • ms-settings:storagesense

COM Automation: For the record you can uninstall a product via COM automation selectively (excluding your product) like this:

Dim installer : Set installer = CreateObject("WindowsInstaller.Installer")
Set products = installer.ProductsEx("", "", 7)

'Iterate over all MSI packages on the box
For Each product In products
   If LCase("{1610CB69-BE80-41B9-9B77-E346C1DA12F3}") = LCase(product.productcode) Then
        MsgBox "Found Product: " + product.InstallProperty("ProductName")
      Else
        ' Can run uninstall here via COM method - ADMIN RIGHTS!
        ' installer.ConfigureProduct product.productcode, 0, 2       
   End If
Next

MsgBox "Finished."

Powershell MSI Module: There is also a Powershell module for MSI made by deployment guru Heath Stewart. Github.com: https://github.com/heaths/psmsi. I have not used it actively, but surely this module has sorted out most problems and will be a better option than going via COM? Personally I would just use a VBScript and COM - easier in my opinion.

Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164
  • OMG thank you so much. Yeah this was definitely the answer I was looking for. Yeah we've been trying to stay out of the Installer folder, but have noticed a few large sized .msi(s) that are really old and are just taking space. We are doing cleanmgr and using storage sense now actually. Thanks for your answer man! – Sean Dobbins Dec 06 '19 at 17:25
  • Do you have products you can not uninstall now then? (tends to happen when you meddle with this folder). If so, see this list: [Get rid of un-uninstallable packages](https://stackoverflow.com/questions/53876550/wix-custom-action-dialogbox-on-silent-uninstall-of-application/53876981#53876981). The entry for [MS Fix It](https://support.microsoft.com/en-us/help/17588/fix-problems-that-block-programs-from-being-installed-or-removed) should be enough for small scale. If the answer was acceptable, please set it accepted by ticking the box on the left. – Stein Åsmul Dec 06 '19 at 17:30
  • Yeah we had an issue with one of our applications because the package was deleted, just didn't want to repeat the issue with other computers for the same application. Ok, I check marked your answer, but it won't let me up vote cause I'm still new in this forum – Sean Dobbins Dec 06 '19 at 17:38
  • That's OK - I'll give you one up-vote for the nicely formatted question. Welcome to the forum. – Stein Åsmul Dec 06 '19 at 17:58