Ironman Software Forums
Continue the conversion on the Ironman Software forums. Chat with over 1000 users about PowerShell, PowerShell Universal, and PowerShell Pro Tools.
Discuss this ArticleIn this blog post, learn how to read the registry with PowerShell.
The registry provider makes reading from the registry similar to accessing a file system.
Registry hives are drives within the registry provider. You can list the hives by using Get-PSDrive.
Get-PSDrive -PSProvider Registry
Navigate to a particular drive with Set-Location or the cd alias.
Set-Location HKCU:\
You can list subkeys with the Get-ChildItem or dir alias.
cd HKCU:\
Get-ChildItem
You can also use the Set-Location or the cd alias to open subkeys.
Set-Location HCKU:\Console
You can get values for a key by using Get-ItemProperty.
Get-ItemProperty HKCU:\Console
You can also list the current path’s values by using Get-ItemProperty without a path.
Set-Location HKCU:\Console
Get-ItemProperty
You can also use the .NET Registry class directly. While it’s not quite as user friendly as the Registry Provider, it does provide more options.
The Registry class has several static fields for accessing hives.
[Microsoft.Win32.Registry]::ClassesRoot
[Microsoft.Win32.Registry]::CurrentUser
[Microsoft.Win32.Registry]::LocalMachine
[Microsoft.Win32.Registry]::PerformanceData
To list subkeys, use the GetSubKeyNames() method.
[Microsoft.Win32.Registry]::CurrentUser.GetSubKeyNames()
To open a subkey, use the OpenSubKey() method. You will need to ensure that you call Dispose() when you are done using it.
$Subkey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey("Console")
$Subkey.Dispose()
You can get the values within a key using GetValue() and GetValueNames() methods.
$Subkey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey("Console")
$Subkey.GetValueNames()
$Subkey.GetValue("LineWrap")
$Subkey.Dispose()
Find this useful? Please consider sharing this article. Have a question about PowerShell? Contact us and we'll write a post about it.
Continue the conversion on the Ironman Software forums. Chat with over 1000 users about PowerShell, PowerShell Universal, and PowerShell Pro Tools.