Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More succinct way of getting a registry value as a string than (Get-ItemProperty $key $valueName)._VALUENAME_?

The method for getting a value in a registry key from PowerShell is:

Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion CommonFilesDir

However, that command returns some extra properties I don't usually want:

CommonFilesDir : C:\Program Files\Common Files
PSPath         : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion
PSParentPath   : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
PSChildName    : CurrentVersion
PSDrive        : HKLM
PSProvider     : Microsoft.PowerShell.Core\Registry

I just want the actual value, a string in this case. To do that I have to use the more verbose:

$commonFilesDir = (Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion CommonFilesDir).CommonFilesDir

Other than writing my own alias, is there a way of not writing the property name twice and getting a string?

I could run the following command, but it returns a PSObject:

Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion | Select CommonFilesDir
like image 608
Justin Dearing Avatar asked May 01 '13 12:05

Justin Dearing


People also ask

How do I find the registry value?

One of the easiest ways to find registry keys and values is using the Get-ChildItem cmdlet. This uses PowerShell to get a registry value and more by enumerating items in PowerShell drives. In this case, that PowerShell drive is the HKLM drive found by running Get-PSDrive .

Which cmdlet can we obtain the item property of registry keys?

The Get-ItemProperty cmdlet gets the properties of the specified items. For example, you can use this cmdlet to get the value of the LastAccessTime property of a file object. You can also use this cmdlet to view registry entries and their values.

Which PowerShell cmdlet is used to modify a registry key value?

You can also use the New-ItemProperty cmdlet to create the registry entry and its value and then use Set-ItemProperty to change the value. For more information about the HKLM: drive, type Get-Help Get-PSDrive . For more information about how to use PowerShell to manage the registry, type Get-Help Registry .

What is registry in PowerShell?

Because registry keys are items on PowerShell drives, working with them is very similar to working with files and folders. One critical difference is that every item on a registry-based PowerShell drive is a container, just like a folder on a file system drive.


2 Answers

I'm new to PowerShell, but it seems to work in PowerShell 2 and 3 if you leave out the registry value name in Get-ItemProperty, using the value name only as a property:

(Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion).CommonFilesDir

or even shorter with the alias:

(gp HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion).CommonFilesDir

No repetition of the value name, clean, and it can't get much more succinct.

like image 131
jlalughery Avatar answered Sep 18 '22 16:09

jlalughery


This is no less clunky, but there's no repetition if that's an itch you need to scratch:

(gi HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion).GetValue("CommonFilesDir")

(personally I'd use $env:commonprogramfiles but that's besides the point.)

like image 37
x0n Avatar answered Sep 20 '22 16:09

x0n