How to resolve "ERROR: Specified cast is not valid." error during installation?

This error appears to be the result of Get-ItemProperty failing. From your log, the part of the script that is failing is here:

  Write-Output "Searching if new version exists..."
  $checkreg64 = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion | Where-Object { $_.DisplayName -like '*Java 8*' -and ([Version]$_.DisplayVersion) -eq $version} -ErrorAction SilentlyContinue
  $checkreg32 = Get-ItemProperty HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion | Where-Object { $_.DisplayName -like '*Java 8*' -and ([Version]$_.DisplayVersion) -eq $version} -ErrorAction SilentlyContinue

This failure occurs when Get-ItemProperty expects to read a registry key of type X and reads a key whose data doesn't match the constraints of the key type. research1 research2 research3

The solution in this case was to find the invalid key in registry (the registry paths that are queried in $checkreg64 and $checkreg32) and manually recreate it as a DWORD with value 1.

Update:

From the comments, you get "Specified cast is not valid" when querying Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*. It is possible there is an unrelated key in this location that contains an invalid subkey, causing the query against this location to fail. We should be able to parse each key individually from the Uninstall location to determine which key we're having trouble querying.

Run the below:

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | % { write-host "Key Name:" $_.PSChildName }

This should return some results, then, will return the Specified cast error. The error will occur on the key harboring the invalid subkey.

For example, if the results of the above look like this:

Key Name: fake_key_name_1
Key Name: fake_key_name_2
Key Name: fake_key_name_3
Get-ItemProperty : Specified cast is not valid.

Then the last key you were able to successfully query was fake_key_name_3. The key we could not query was the next key in the list. Open regedit and browse to the next key name (presumably, fake_key_name_4). This is where the failure is. There should be an invalid subkey here. Fix this, then run the command again. If you get no errors, you're all set. If you get more keys with errors, find and fix their invalid subkeys.

In the case of one of the linked examples I originally provided, the user would expect to find a REG_DWORD key with data = "(invalid DWORD (32-bit) value)". This is the key to fix.


I just had this problem. I did:

get-childitem hklm:\software\microsoft\windows\currentversion\uninstall\ |
  foreach { write-host $_.pspath; $_ } | get-itemproperty

and it choked on HKLM:\software\microsoft\windows\currentversion\uninstall\nbi-nb-base-8.2.0.0.201609300101, which is for Netbeans 8.2. I see in regedit that NoModify has "(invalid DWORD (32-bit) value)". get-itemproperty -erroraction continue has no effect.

EDIT: The Netbeans people are finally fixing this currently. https://issues.apache.org/jira/browse/NETBEANS-2523