Reading the Windows registry from PowerShell can be a bit cumbersome, when all you wanted to retrieve is the value of an item under a key, at least with PowerShell version before v5.
The Get-ItemProperty can be used to enumerate the items and their values under a registry key, but retrieving a single item (previous to PS v.5) involved accessing a custom property in the returned PSCustomObject.
Only with PS v.5 the Get-ItemPropertyValue cmdlet was introduced, which actually returns the value only.
However, on Windows 2008/R2 servers, it is not always possible to update PowerShell and the .Net framework.
A “plain” call to Get-ItemProperty with a named item will return all information about the item under the specified key.
This example returns all properties of the item, including its value:
Get-ItemProperty -Path "Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Programs" -Name "NoWindowsFeatures"
The following PSCustomObject is returned:
To actually retrieve the item’s value only, you’d need to do something like:
(Get-ItemProperty -Path "Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Programs" -Name "NoWindowsFeatures").NoWindowsFeatures
Of course, it is not very nice to have to repeat the item’s name.
To get around this annoyance, I have the following simple workaround helper function, which will be defined only if the PowerShell version is less than 5:
# PS V4- if ($psversiontable.PSVersion.Major -lt 5) { function Get-ItemPropertyValue() { Param ( [parameter()] [String] $Path, [parameter()] [String] $Name ) (Get-ItemProperty -LiteralPath $Path -Name $Name).$Name } }
So, now also in PowerShell v3 and 4, you can just call:
Get-ItemPropertyValue -Path "Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Programs" -Name "NoWindowsFeatures"