Thursday, November 19, 2015

Working with registry values in PowerShell 5

Working with the registry in PowerShell has always kind of sucked. Now, in PowerShell 5, it’s, well, different, a little.

It’s mostly the same, but there is one new command, which does nothing new, but does it in a new way. I guess that’s progress.

To get a registry key, you still use Get-Item.

$Key = Get-Item HKLM:\SOFTWARE\7-zip

To get a registry value, you still use Get-ItemProperty to get all of the values in a key, and pipe them to Select-Object to get just the value you want, in this case, a value named “Path”.

$Value = Get-ItemProperty HKLM:\SOFTWARE\7-zip | Select Path

That results in a custom object with a property named Path. So if what you really need to work with the Data in the Value (stupid registry terminology), you would previously use this.

$Data = Get-ItemProperty HKLM:\SOFTWARE\7-zip | Select -ExpandProperty Path

Or this.

$Data = (Get-ItemProperty HKLM:\SOFTWARE\7-zip).Path

In PowerShell 5, you can now also do it this way.

$Data = Get-ItemPropertyValue HKLM:\SOFTWARE\7-zip -Name Path

No comments:

Post a Comment