Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the value of a registry key and ONLY the value using powershell

Can anyone help me pull the value of a registry key and place it into a variable in PowerShell? So far I have used Get-ItemProperty and reg query and although both will pull the value, both also add extra text. I need just the string text from the registry key and ONLY the string text from the key. I'm sure I could create a function to strip off the extra text but if something changes (i.e. reg key name) it might affect this.

like image 632
Alan Anderson Avatar asked Mar 19 '13 22:03

Alan Anderson


People also ask

How do I check the value of my registry?

Click Start or press the Windows key . In the Start menu, either in the Run box or the Search box, type regedit and press Enter . In Windows 8, you can type regedit on the Start screen and select the regedit option in the search results. In Windows 10, type regedit in the Search box on the taskbar and press Enter .

How do you access the registry in PowerShell?

HKLM:\ and HKCU:\ are used to access a specific registry hive. Those, you can access the registry key and their parameters using the same PowerShell cmdlets that you use to manage files and folders. To refer to registry keys, use cmdlets with xxx-Item: Get-Item – get a registry key.

How do you locate keys and their values in the registry?

Regedit lets you search using the Find feature in the Windows Registry. To access it, you have to click on Edit menu and select Find. The Find box will let you search for items in the Windows Registry, and this includes Keys, Values, and Data. You can also set it to match whole strings only.

Which command modifies the value of a key in the registry Editor in PowerShell?

To modify the value of a registry property value requires using the Set-PropertyItem cmdlet. Modifying the value of a registry property value: Use the Push-Location cmdlet to save the current working location. Use the Set-Location cmdlet to change to the appropriate registry drive.


2 Answers

$key = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion' (Get-ItemProperty -Path $key -Name ProgramFilesDir).ProgramFilesDir 

I've never liked how this was provider was implemented like this : /

Basically, it makes every registry value a PSCustomObject object with PsPath, PsParentPath, PsChildname, PSDrive and PSProvider properties and then a property for its actual value. So even though you asked for the item by name, to get its value you have to use the name once more.

like image 175
Andy Arismendi Avatar answered Oct 01 '22 17:10

Andy Arismendi


NONE of these answers work for situations where the value name contains spaces, dots, or other characters that are reserved in PowerShell. In that case you have to wrap the name in double quotes as per http://blog.danskingdom.com/accessing-powershell-variables-with-periods-in-their-name/ - for example:

PS> Get-ItemProperty Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7  14.0         : C:\Program Files (x86)\Microsoft Visual Studio 14.0\ 12.0         : C:\Program Files (x86)\Microsoft Visual Studio 12.0\ 11.0         : C:\Program Files (x86)\Microsoft Visual Studio 11.0\ 15.0         : C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\ PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\V                S7 PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS PSChildName  : VS7 PSProvider   : Microsoft.PowerShell.Core\Registry 

If you want to access any of the 14.0, 12.0, 11.0, 15.0 values, the solution from the accepted answer will not work - you will get no output:

PS> (Get-ItemProperty Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7 -Name 15.0).15.0 PS> 

What does work is quoting the value name, which you should probably be doing anyway for safety:

PS> (Get-ItemProperty "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7" -Name "15.0")."15.0" C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\ PS>  

Thus, the accepted answer should be modified as such:

PS> $key = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7" PS> $value = "15.0" PS> (Get-ItemProperty -Path $key -Name $value).$value C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\ PS>  

This works in PowerShell 2.0 through 5.0 (although you should probably be using Get-ItemPropertyValue in v5).

like image 24
Ian Kemp Avatar answered Oct 01 '22 16:10

Ian Kemp