Home > front end >  Powershell: Cannot access registry value
Powershell: Cannot access registry value

Time:11-11

I need to get value for "UninstallString" in

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall {1535CAA3-9F33-414E-8987-0365169BE741}

calling:

Get-Item -path HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{1535CAA3-9F33-414E-8987-0365169BE741}

results in Get-Item : A positional parameter cannot be found that accepts argument '1535CAA3-9F33-414E-8987-0365169BE741'.

CodePudding user response:

Something like this with quotes, since curly brackets look like a scriptblock to powershell:

get-itemproperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{013DB423-A8DE-4423-9E50-D45ED1041789}' uninstallstring | 
  % uninstallstring

MsiExec.exe /I{013DB423-A8DE-4423-9E50-D45ED1041789}

Although for an msi you can uninstall with this in powershell 5.1:

get-package *chrome* | uninstall-package

For other non-msi installs the uninstallstring is here. Usually you have to add an extra option like "/S" for silent uninstall.

get-package *firefox* | % { $_.metadata['uninstallstring'] }

"C:\Program Files\Mozilla Firefox\uninstall\helper.exe"

CodePudding user response:

To complement js2010's helpful answer, which notes that lack of quoting is your primary problem ({ and } are PowerShell metacharacters):

In PowerShell v5 and above, you can use the Get-ItemPropertyValue cmdlet to directly return the data associated with a registry key's value:

# Note the use of '...' around the registry path 
# and the value name UninstallString at the end.
Get-ItemPropertyValue 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{1535CAA3-9F33-414E-8987-0365169BE741}' UninstallString

As for what you tried:

An unquoted argument such as HKLM:\...\{1535CAA3-9F33-414E-8987-0365169BE741} is processed as follows:

  • Because {...} creates a script block, it is considered the start of a separate argument, and string 'HKLM:\...\' and script block {1535CAA3-9F33-414E-8987-0365169BE741} are passed as two arguments.

  • Because Get-Item didn't expect an extra (positional) argument, it complained accordingly, and used the string representation of the script block in its error message.

    • A script block's string representation is its verbatim content, excluding { and }, which you can verify with {1535CAA3-9F33-414E-8987-0365169BE741}.ToString()

As noted, quoting is the solution, and since the path contains neither PowerShell variable references nor subexpressions, using a single-quoted, i.e. verbatim string literal ('...') is best.

The (usually less desirable) alternative is to stick with an unquoted argument and use individual escaping of PowerShell metacharacters with ` (the so-called backtick); e.g.:
Write-Output HKLM:\...\`{1535CAA3-9F33-414E-8987-0365169BE741`}

CodePudding user response:

As I commented you need to wrap the path in "" however that still won't work. To get a registry value in powershell use Registry::HKEY_LOCAL_MACHINE\... as the path. And once you get the RegistryKey object you can use GetValue(string name) to get the value of UninstallString. So your command would look something like this:

(Get-Item -path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{1535CAA3-9F33-414E-8987-0365169BE741}").GetValue("UninstallString")
  • Related