How to resolve "ERROR: Specified cast is not valid." error during installation?
Solution 1
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
Solution 2
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.
Related videos on Youtube
![user598527](https://i.stack.imgur.com/AIgMV.png?s=256&g=1)
user598527
If I haven't accepted your correct answer I likely haven't yet been able to verify it — I only mark answers as accepted when either a reliable source is provided or I'm capable confirming myself. Additionally in some questions there can be multiple equally great answers, please also keep this in mind.
Updated on September 18, 2022Comments
-
user598527 almost 2 years
I'm currently receiving this error when installing Java SE Runtime Environment (
jre8
), updating used to work fine:Searching if new version exists... ERROR: Specified cast is not valid. The install of jre8 was NOT successful. Error while running 'C:\ProgramData\chocolatey\lib\jre8\tools\chocolateyInstall.ps1'. See log for details. Chocolatey installed 0/1 packages. 1 packages failed. See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log). Failures - jre8 (exited -1) - Error while running 'C:\ProgramData\chocolatey\lib\jre8\tools\chocolateyInstall.ps1'. See log for details.
chocolatey.log(I can't paste here because of the 30 000 character length limit, I've deleted the dates)-
user598527 about 4 yearsThe Pastebin log file has been deleted, apparently I somehow set expiration for the paste.
-
-
user598527 almost 6 yearsCan you update the answer with full paths? I've navigated to
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
in the registry editor, but I don't know how to continue from there. -
root almost 6 yearsFor the x64 example: Run
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object { $_.DisplayName -like '*Java 8*' } | select -expandproperty PSChildName
. If the result is {12345}, check for keyHKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\{12345}\NoModify
. -
user598527 almost 6 yearsI get this error for the command:
Get-ItemProperty : Specified cast is not valid. At line:1 char:1 + Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Unin ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-ItemProperty], InvalidCastException + FullyQualifiedErrorId : System.InvalidCastException,Microsoft.PowerShell.Commands.GetItemPropertyCommand
-
root almost 6 yearsGreat, we've inadvertently confirmed (at least one of) the location(s) of the problem. Manually navigate to HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\ through regedit, browsing each subkey for the one containing the DisplayName like "Java 8". That's where NoModify will be. Let me know if you'd like to move this discussion to chat.
-
user598527 almost 6 yearsThe issue is that I can't effective search registry — but that's another question. I've searched for "java" in Regedit, but only entries in Notepad++ folder are found.
-
user598527 almost 6 yearsThe maintainer's Chocolatey package Github page: github.com/proudcanadianeh/ChocoPackages/issues/18
-
root almost 6 yearsI've updated my answer to include more troubleshooting steps.
-
root almost 6 yearsDid you get anywhere with this?
-
user598527 almost 6 yearsSorry, I put the topic on hold for a while as the process is getting demanding for my skill level — I'll try to finish early next week. Thank you for the concern.
-
js2010 about 5 yearsThis didn't work well for me. I had to skip 3 keys down from MozillaMaintenanceService to nbi-nb-base-8.2.0.0.201609300101 to find the offending NoModify value from Netbeans.
-
user598527 almost 5 years@js2010: Stopped at Mozilla Maintenance Service for me as well.
-
user598527 almost 5 yearsThe Regedit path was
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\nbi-tmcbeans-1.0.0.0.0
for me. -
user598527 almost 5 yearsDeleting the
NoModify
value allowed installing jre8 immediately. -
user598527 almost 5 yearsThank you for pointing to the right direction with your answer, the question has now been solved.
-
IT_User over 2 yearsnewbedev.com/… This link is what explained the issue and it was netbeans 8.2. issues.apache.org/jira/browse/NETBEANS-2523
-
js2010 over 2 years@IT_User Yes I posted that bug, and in Netbeans 12.6 it is STILL not resolved.