Skip to content

Instantly share code, notes, and snippets.

@heaths
Last active January 24, 2023 17:07
Embed
What would you like to do?
Removes Windows Installer product registration for products where the locally cached MSI is missing and no registered source can be found.
#Requires -Version 3
# The MIT License (MIT)
# Copyright (C) Microsoft Corporation. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(Position = 0, ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[string[]] $ProductCode
)
$ErrorActionPreference = 'Stop'
[int[]] $translation = 7,6,5,4,3,2,1,0,11,10,9,8,15,14,13,12,17,16,19,18,21,20,23,22,25,24,27,26,29,28,31,30
$loc = data {
ConvertFrom-StringData @'
Error_Elevation_Required = You must run this script in an elevated command prompt
Error_64Bit_Required = You must run this in a 64-bit command prompt
Error_PackageManagement_Required = Please install PackageManagement from http://go.microsoft.com/fwlink/?LinkID=746217
Process_Remove_Args1 = Remove registration for {0}
Verbose_Install_MSI = Installing the "MSI" module
Verbose_Scan_Missing = Scanning for products missing cached packages
Verbose_Remove_Key_Args1 = Removing registry key {0}
Verbose_Remove_Value_Args2 = Removing registry value {1} from {0}
Verbose_Remove_Source_Reg = Removing source registration
Verbose_Remove_Product_Reg = Removing product registration
Verbose_Remove_Upgrade_Reg = Removing upgrade registration
Verbose_Remove_Component_Reg = Removing component registration
Verbose_Found_Source_Args2 = Cache missing for {0} but found source at {1}
'@
}
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object System.Security.Principal.WindowsPrincipal $identity
if (!$principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) {
throw $loc.Error_Elevation_Required
}
if ([System.Environment]::Is64BitOperatingSystem) {
if (![System.Environment]::Is64BitProcess) {
throw $loc.Error_64Bit_Required
}
}
$pack = {
param (
[string] $Guid
)
if (!$Guid) {
return
}
$Guid = (New-Object System.Guid $Guid).ToString("N").ToUpperInvariant()
$sb = New-Object System.Text.StringBuilder $translation.Count
foreach ($i in $translation) {
$null = $sb.Append($Guid[$i])
}
$sb.ToString()
}
$test = {
param (
$Product
)
if ($Product.PSPath -and ($Product | Test-Path)) {
return $true
}
if ($Product.PackageName) {
$Product | Get-MSISource | ForEach-Object {
$path = Join-Path $_.Path $Product.PackageName
if ($path | Test-Path) {
Write-Verbose ($loc.Verbose_Found_Source_Args2 -f $Product.ProductCode, $path)
return $true
}
}
}
$false
}
$remove = {
param (
[string] $Key
)
if (Test-Path $Key) {
Write-Verbose ($loc.Verbose_Remove_Key_Args1 -f $Key)
Remove-Item -Recurse -Force -UseTransaction $Key
}
}
$removeChild = {
param (
[string] $Key,
[string] $Name
)
if (Test-Path $Key) {
Get-ChildItem $Key | ForEach-Object {
$obj = $_ | Get-ItemProperty
if ($obj.$Name -ne $null) {
Write-Verbose ($loc.Verbose_Remove_Value_Args2 -f $_.Name, $Name)
Remove-ItemProperty -Force -UseTransaction -Name $Name -LiteralPath $_.PSPath
$obj = Get-ItemProperty -UseTransaction -LiteralPath $_.PSPath
if (!$obj) {
Write-Verbose ($loc.Verbose_Remove_Key_Args1 -f $_.Name)
Remove-Item -Recurse -Force -UseTransaction -LiteralPath $_.PSPath
}
}
}
}
}
if (!$ProductCode) {
# Install the MSI module if missing.
if (!(Get-Module -ListAvailable MSI)) {
Write-Verbose $loc.Verbose_Install_MSI
# Make sure PackageManagement is installed (comes with WMF 5.0 / Windows 10).
if (!(Get-Module -ListAvailable PackageManagement)) {
throw $loc.Error_PackageManagement_Required
}
Install-Module MSI -Scope CurrentUser -SkipPublisherCheck -Force
}
Write-Verbose $loc.Verbose_Scan_Missing
foreach ($msi in (Get-MSIProductInfo -UserContext Machine)) {
if (!(&$test $msi)) {
$ProductCode += $msi.ProductCode
}
}
}
foreach ($code in $ProductCode) {
if ($PSCmdlet.ShouldProcess($msi, $loc.Process_Remove_Args1 -f $code)) {
$packedProductCode = &$pack $code
Start-Transaction
Write-Verbose $loc.Verbose_Remove_Source_Reg
&$remove "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products\$packedProductCode"
&$remove "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Features\$packedProductCode"
Write-Verbose $loc.Verbose_Remove_Product_Reg
&$remove "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\$packedProductCode"
&$remove "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$code"
&$remove "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\$code"
Write-Verbose $loc.Verbose_Remove_Upgrade_Reg
&$removeChild "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UpgradeCodes" $packedProductCode
Write-Verbose $loc.Verbose_Remove_Component_Reg
&$removeChild "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components" $packedProductCode
Complete-Transaction
}
}
<#
.SYNOPSIS
Removes Windows Installer product registrtation for missing or specified MSIs
.DESCRIPTION
If Windows Installer product registration is corrupt (exit code 1610) or package
sources are missing (exit code 1603, error message 1714; or exit code 1612),
you can use this script in an elevated PowerShell command shell to clean up the
registration is a transactional manner to avoid making machine state worse.
Please note that this should be a last resort and only for those issues above.
The old msizap.exe program was frought with issues and can make matters worse
if not used properly.
.PARAMETER ProductCode
Optional list of ProductCode to clean up; otherwise, ProductCodes are scanned
from products with missing sources.
.EXAMPLE
PS> Unregister-MissingMSIs.ps1
Removes per-machine product registration for products with missing cached MSIs.
.EXAMPLE
PS> Unregister-MissingMSIs.ps1 '{7B88D6BB-A664-4E5A-AB81-C435C8639A4D}'
Remove per-machine product registration for the specified ProductCode only.
#>
@BrentHumphreys
Copy link

A slight modification to your script to get it to run https://gist.github.com/BrentHumphreys/111ac670e577a94bf1531cc4d9a9d8a0

@Daniel15
Copy link

Is this doing the same thing as the old Windows Installer CleanUp Utility (MSICUU2.exe)?

@zachsa
Copy link

zachsa commented Oct 27, 2017

I found I was only able to run the script if it was placed at C:\<something>.ps1

@FY1043031060
Copy link

don't effect

@heaths
Copy link
Author

heaths commented Nov 20, 2017

@BrentHumphreys the script runs fine on a number of machines. If you have feedback, I'd appreciate knowing so I can fix it rather than forking a gist (that doesn't enjoy the same PR treatment full repos get, nor do I get notifications that any of these comments even occurred).

@koly86
Copy link

koly86 commented Mar 7, 2018

unreg1

@RURon
Copy link

RURon commented Apr 20, 2018

@koly86 YOu need to set Execution policy to allow you to run a script. One way "Set-ExecutionPolicy Bypass -Scope Process"

@BillGatesIII
Copy link

Thanks, great work. This fixed my 'The older version of Microsoft Visual C++ ... Debug Runtime ... cannot be removed' error.

@marsonfire
Copy link

Thank you, this is the only thing that worked for me.
Steps I did in case anyone else needs it:

  1. Run PowerShell in Admin Mode
  2. Copy and Paste: "Set-ExecutionPolicy Bypass -Scope Process" and hit enter
  3. You may have to install Nuget packages, just do it.
  4. Copy and paste script above
    It took a little for my computer to load and do stuff, but that might be because it's slow

@mti5914-zz
Copy link

image
Run the script in PowerShell and get the error.

@xXLinNix
Copy link

Thanks! Works perfect on Win10 1809

@SummerDiver
Copy link

image
Run the script in PowerShell and get the error.

I have the same problem, have u solved it?

@myrkjartan
Copy link

I had the same problem. I got the script to run till the end by wrapping line 100 in try/catch
Try{ Remove-Item -Recurse -Force -UseTransaction $Key } Catch{ continue }
I still can't install visual studio though

@heaths
Copy link
Author

heaths commented Jul 17, 2019

@myrkjartan Please use the feedback button in the upper-right of the installer to file a ticket so we can investigate further.

@pandit77
Copy link

Thanks man. That finally fixed the Issue for me!

@dmcdivitt
Copy link

dmcdivitt commented Jul 29, 2019

PS C:\windows\system32> C:\Users\david.mcdivitt\Desktop\Unregister-MissingMSIs.ps1 Join-Path : Cannot find drive. A drive with the name 'https' does not exist. At C:\Users\david.mcdivitt\Desktop\Unregister-MissingMSIs.ps1:82 char:21 + $path = Join-Path $_.Path $Product.PackageName + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (https:String) [Join-Path], DriveNotFoundException + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.JoinPathCommand

@kdschlosser
Copy link

ok folks. this script above appears to be for window 10 and is build specific possibly I am not sure I know it does not work properly on Windows 7 even after i did some small modifications it would find the registry value then it would say it doesn't exist when it went to delete it. and the ones it didn't error on it simply did not remove them.

You are going to have to go old school to get this done

there are 2 modules that caused me issues. I listed them below with their associated registry keys. The keys that have S-1-5-18 do have to be deleted. I do not know if the last portion of the key is the same for everyone or not. This is simple, open your favorite registry editor enter the key. Export the thing to a file (meh.. ya never know...) then delete the key.

I am not sure why I have a key marked Microsoft.VisualStudio.Setup.Configuration.1 so you may want to do a registry search for the 2 modules named to be sure you do not have keys like that in there as well.

Once I did this everything was smooth sailing..

  • Microsoft.VisualStudio.Setup.Configuration

    • HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Dependencies\Microsoft.VisualStudio.Setup.Configuration,v15
    • HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products\9EFE8B32617806543B7DBEFADCDF522A
    • HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Microsoft.VisualStudio.Setup.Configuration
    • HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Microsoft.VisualStudio.Setup.Configuration.1
    • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\6A596D53F7EF1CB50AA5BDD55B952C6B
    • HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall{23B8EFE9-8716-4560-B3D7-EBAFCDFD25A2}
    • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\9EFE8B32617806543B7DBEFADCDF522A
  • Microsoft.Build.FileTracker.Msi

    • HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Dependencies\Microsoft.Build.FileTracker.Msi,v15
    • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\F831E14AF3A5C3447BD259A79B49BFA5
    • HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall{A41E138F-5A3F-443C-B72D-957AB994FB5A}
    • HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products\F831E14AF3A5C3447BD259A79B49BFA5

@heaths
Copy link
Author

heaths commented Aug 11, 2019

The Windows Installer registry layout is the same across all versions of Windows, barring slight difference between 9x and NT. But this script isn't designed to handle all possible forms of corruption. Best case scenario is to avoid using System Restore and any apps that touch the %WINDIR%\Installer directory (CCleaner at least used to do this, as did many others lesser-known cleaners I found years back).

@kdschlosser
Copy link

The thing is in order to know 100% how to clean out Visual Studio you have to start off with a fresh install of Windows. create a snapshot of the registry. install Visual Studio and every single extension module/package then take another snapshot and compare the 2. find the differences and then write a script to do the cleanup. It actually would not be a bad thing to have because VS leaves all kinds of stale registry entries when you uninstall it. If you have a machine that has been running on the same copy of Windows for almost a decade as I do. Visual Studio has made a disaster of the registry (along with a lot of other programs). I do not understand why developers that build installers do not understand that uninstall means everything as in all, registry entries, temp files, program files, app data, every thing it created on the computer that is not considered user data.

I also do not understand the need to have that "Install" directory. it is just a script that runs that removes everything there is no need to store the whole damned installer there and occupy so much space. I have not tested this But knowing what we all know about Microsoft, I am willing to bet that once the removal has been done that installer file is still sitting in that directory and doesn't get removed. And because of the nice thing that gets done with the change of the file name to a collection of random numbers and letters you have no way to identify what can and cannot be removed. I have seen machines where this single directory is chewing up close to 100 gig.

When i started dealing with this problem on my machine I had 2017 Build Tools installed I ran the VS installer and it stated that the installation of Visual Studio 2017 was going to take up 18 gig of space with only the IDE, C++ worker, and the windows 10 SDK installed., I am not sure why it was going to consume so much drive space. when I ran the VS 2019 installer it cam up to 4.25 gig for the exact same packages selected.

@alexpospelov
Copy link

i have this issue, but script doesn't work in my version Microsoft Powershell. I think reinstall my OS windows 7.

@heaths
Copy link
Author

heaths commented May 4, 2020

@alexpospelov, can you clarify what doesn't work, e.g. what error did you see? More information is required to help troubleshoot.

@disini
Copy link

disini commented May 16, 2020

When I run the shell it shows "+ CategoryInfo : ObjectNotFound: (Q:String) [Join-Path], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.JoinPathCommand"
,and I have plugged all my 5 protable hard drives(M,N,O,Q,U) into my USB hub, and it shows that warning again and again.
Is this a bug?
Could you please fix it? Much appreciate!

`Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6 PS C:\WINDOWS\system32> L: PS L:> cd L:\gist.github.com\heaths PS L:\gist.github.com\heaths> dir

目录: L:\gist.github.com\heaths

Mode LastWriteTime Length Name


-a---- 2020/5/16 21:51 7658 local.ps1
-a---- 2020/5/16 21:51 7658 Unregister-MissingMSIs.ps1

PS L:\gist.github.com\heaths> Unregister-MissingMSIs.ps1
Unregister-MissingMSIs.ps1 : 无法将“Unregister-MissingMSIs.ps1”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请
检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
所在位置 行:1 字符: 1

  • Unregister-MissingMSIs.ps1
  •   + CategoryInfo          : ObjectNotFound: (Unregister-MissingMSIs.ps1:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException
    
    
    

Suggestion [3,General]: 找不到命令 Unregister-MissingMSIs.ps1,但它确实存在于当前位置。默认情况下,Windows PowerShell 不会从当前位置加载命令。如果信任此命令,请改为键入“.\Unregister-MissingMSIs.ps1”。有关详细信息,请参阅 "get-help about_Command_Precedence"。
PS L:\gist.github.com\heaths> .\Unregister-MissingMSIs.ps1

需要使用 NuGet 提供程序来继续操作
PowerShellGet 需要使用 NuGet 提供程序“2.8.5.201”或更高版本来与基于 NuGet 的存储库交互。必须在“C:\Program
Files\PackageManagement\ProviderAssemblies”或“C:\Users\disini\AppData\Local\PackageManagement\ProviderAssemblies”中
提供 NuGet 提供程序。也可以通过运行 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force' 安装 NuGet
提供程序。是否要让 PowerShellGet 立即安装并导入 NuGet 提供程序?
[Y] 是(Y) [N] 否(N) [S] 暂停(S) [?] 帮助 (默认值为“Y”):
Join-Path : 找不到驱动器。名为“Q”的驱动器不存在。
所在位置 L:\gist.github.com\heaths\Unregister-MissingMSIs.ps1:82 字符: 21

  •         $path = Join-Path $_.Path $Product.PackageName
    
  •                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ObjectNotFound: (Q:String) [Join-Path], DriveNotFoundException
    • FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.JoinPathCommand

PS L:\gist.github.com\heaths> .\Unregister-MissingMSIs.ps1
Join-Path : 找不到驱动器。名为“o”的驱动器不存在。
所在位置 L:\gist.github.com\heaths\Unregister-MissingMSIs.ps1:82 字符: 21

  •         $path = Join-Path $_.Path $Product.PackageName
    
  •                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ObjectNotFound: (o:String) [Join-Path], DriveNotFoundException
    • FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.JoinPathCommand

PS L:\gist.github.com\heaths> .\Unregister-MissingMSIs.ps1
Join-Path : 找不到驱动器。名为“P”的驱动器不存在。
所在位置 L:\gist.github.com\heaths\Unregister-MissingMSIs.ps1:82 字符: 21

  •         $path = Join-Path $_.Path $Product.PackageName
    
  •                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ObjectNotFound: (P:String) [Join-Path], DriveNotFoundException
    • FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.JoinPathCommand

PS L:\gist.github.com\heaths>`

@heaths
Copy link
Author

heaths commented May 18, 2020

Windows Installer registered components to those drive letters, and they would have to be found. There's nothing I can fix. It's broken registration due to products not properly directing components to something other than TARGETDIR. Look at results in https://www.bing.com/search?q=TARGETDIR+site%3Adevblogs.microsoft.com%2Fsetup for options.

@rsdev88
Copy link

rsdev88 commented Sep 16, 2020

I've spent all day trying to upgrade from VS2017 to VS2019 but getting nowhere because of a missing C++ redistributable (vc_runtime minimum v14.11.25325). I tried everything including removing every trace of existing VS installs and C++ redists that I could find with no luck, but your script did the trick! Thank you very much 👍

@disini
Copy link

disini commented Sep 21, 2020

Windows Installer registered components to those drive letters, and they would have to be found. There's nothing I can fix. It's broken registration due to products not properly directing components to something other than TARGETDIR. Look at results in https://www.bing.com/search?q=TARGETDIR+site%3Adevblogs.microsoft.com%2Fsetup for options.

Thanks so much sir! I've reinstall the Windows and VS2019, it works well now.

@wizicer
Copy link

wizicer commented Jul 1, 2021

Here is my modified version, if you encounter same error as me: Remove-Item : Cannot find path ...

#Requires -Version 3

# The MIT License (MIT) 
# Copyright (C) Microsoft Corporation. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

[CmdletBinding(SupportsShouldProcess = $true)]
param (
    [Parameter(Position = 0, ValueFromPipeline = $true)]
    [ValidateNotNullOrEmpty()]
    [string[]] $ProductCode
)

$ErrorActionPreference = 'Stop'
[int[]] $translation = 7,6,5,4,3,2,1,0,11,10,9,8,15,14,13,12,17,16,19,18,21,20,23,22,25,24,27,26,29,28,31,30

$loc = data {
    ConvertFrom-StringData @'
        Error_Elevation_Required = You must run this script in an elevated command prompt
        Error_64Bit_Required = You must run this in a 64-bit command prompt
        Error_PackageManagement_Required = Please install PackageManagement from http://go.microsoft.com/fwlink/?LinkID=746217
        Process_Remove_Args1 = Remove registration for {0}
        Verbose_Install_MSI = Installing the "MSI" module
        Verbose_Scan_Missing = Scanning for products missing cached packages
        Verbose_Remove_Key_Args1 = Removing registry key {0}
        Verbose_Remove_Value_Args2 = Removing registry value {1} from {0}
        Verbose_Remove_Source_Reg = Removing source registration
        Verbose_Remove_Product_Reg = Removing product registration
        Verbose_Remove_Upgrade_Reg = Removing upgrade registration
        Verbose_Remove_Component_Reg = Removing component registration
        Verbose_Found_Source_Args2 = Cache missing for {0} but found source at {1}
'@
}

$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object System.Security.Principal.WindowsPrincipal $identity
if (!$principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) {
    throw $loc.Error_Elevation_Required
}

if ([System.Environment]::Is64BitOperatingSystem) {
    if (![System.Environment]::Is64BitProcess) {
        throw $loc.Error_64Bit_Required
    }
}

$pack = {
    param (
        [string] $Guid
    )

    if (!$Guid) {
        return
    }

    $Guid = (New-Object System.Guid $Guid).ToString("N").ToUpperInvariant()

    $sb = New-Object System.Text.StringBuilder $translation.Count
    foreach ($i in $translation) {
        $null = $sb.Append($Guid[$i])
    }

    $sb.ToString()
}

$test = {
    param (
        $Product
    )

    if ($Product.PSPath -and ($Product | Test-Path)) {
        return $true
    }

    if ($Product.PackageName) {
        $Product | Get-MSISource | ForEach-Object {
            $path = Join-Path $_.Path $Product.PackageName
            if ($path | Test-Path) {
                Write-Verbose ($loc.Verbose_Found_Source_Args2 -f $Product.ProductCode, $path)
                return $true
            }
        }
    }

    $false
}

$remove = {
    param (
        [string] $Key
    )

    if (Test-Path $Key) {
        Write-Verbose ($loc.Verbose_Remove_Key_Args1 -f $Key)
        Remove-Item -Recurse -Force $Key
    }
}

$removeChild = {
    param (
        [string] $Key,
        [string] $Name
    )

    if (Test-Path $Key) {
        Get-ChildItem $Key | ForEach-Object {
            $obj = $_ | Get-ItemProperty
            if ($obj.$Name -ne $null) {
                Write-Verbose ($loc.Verbose_Remove_Value_Args2 -f $_.Name, $Name)
                Remove-ItemProperty -Force -Name $Name -LiteralPath $_.PSPath

                $obj = Get-ItemProperty -LiteralPath $_.PSPath
                if (!$obj) {
                    Write-Verbose ($loc.Verbose_Remove_Key_Args1 -f $_.Name)
                    Remove-Item -Recurse -Force -LiteralPath $_.PSPath
                }
            }
        }
    }
}

if (!$ProductCode) {
    # Install the MSI module if missing.
    if (!(Get-Module -ListAvailable MSI)) {
        Write-Verbose $loc.Verbose_Install_MSI

        # Make sure PackageManagement is installed (comes with WMF 5.0 / Windows 10).
        if (!(Get-Module -ListAvailable PackageManagement)) {
            throw $loc.Error_PackageManagement_Required
        }

        Install-Module MSI -Scope CurrentUser -SkipPublisherCheck -Force
    }

    Write-Verbose $loc.Verbose_Scan_Missing
    foreach ($msi in (Get-MSIProductInfo -UserContext Machine)) {
        if (!(&$test $msi)) {
            $ProductCode += $msi.ProductCode
        }
    }
}

foreach ($code in $ProductCode) {
    if ($PSCmdlet.ShouldProcess($msi, $loc.Process_Remove_Args1 -f $code)) {
        $packedProductCode = &$pack $code

        Start-Transaction

        Write-Verbose $loc.Verbose_Remove_Source_Reg
        &$remove "HKLM:\SOFTWARE\Classes\Installer\Products\$packedProductCode"
        &$remove "HKLM:\SOFTWARE\Classes\Installer\Features\$packedProductCode"

        Write-Verbose $loc.Verbose_Remove_Product_Reg
        &$remove "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\$packedProductCode"
        &$remove "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$code"
        &$remove "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\$code"

        Write-Verbose $loc.Verbose_Remove_Upgrade_Reg
        &$removeChild "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UpgradeCodes" $packedProductCode
        
        Write-Verbose $loc.Verbose_Remove_Component_Reg
        &$removeChild "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components" $packedProductCode

        Complete-Transaction
    }
}

<#
.SYNOPSIS
Removes Windows Installer product registrtation for missing or specified MSIs

.DESCRIPTION
If Windows Installer product registration is corrupt (exit code 1610) or package
sources are missing (exit code 1603, error message 1714; or exit code 1612),
you can use this script in an elevated PowerShell command shell to clean up the
registration is a transactional manner to avoid making machine state worse.

Please note that this should be a last resort and only for those issues above.
The old msizap.exe program was frought with issues and can make matters worse
if not used properly.

.PARAMETER ProductCode
Optional list of ProductCode to clean up; otherwise, ProductCodes are scanned
from products with missing sources.

.EXAMPLE
PS> Unregister-MissingMSIs.ps1

Removes per-machine product registration for products with missing cached MSIs.

.EXAMPLE
PS> Unregister-MissingMSIs.ps1 '{7B88D6BB-A664-4E5A-AB81-C435C8639A4D}'

Remove per-machine product registration for the specified ProductCode only.
#>

@Limonaire
Copy link

Limonaire commented May 30, 2022

Windows Installer registered components to those drive letters, and they would have to be found. There's nothing I can fix. It's broken registration due to products not properly directing components to something other than TARGETDIR. Look at results in https://www.bing.com/search?q=TARGETDIR+site%3Adevblogs.microsoft.com%2Fsetup for options.

Thanks so much sir! I've reinstall the Windows and VS2019, it works well now.

你好 请问您是怎么做到的?
谢谢了!我也是到这一步就停止了
hi how to repair TARGETDIR thank you

@heaths
Copy link
Author

heaths commented May 31, 2022

@Limonaire could you be more specific? TARGETDIR is just a property. What error are you actually getting? If it's referencing a drive letter you no longer have, there's not much that can be done in any supported manner. This was a bug caused by an MSI that used TARGETDIR improperly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment