Automation

PowerShell Get Registry Value

The Windows Registry and registry editor have long been tools Windows admins have used to resolve issues, apply settings, etc. However, PowerShell makes automating registry changes or programmatically getting registry keys and their values very easy. PowerShell can interact with registry keys and easily get registry values.

What is PowerShell?

Most are familiar with PowerShell. However, just as a refresher, Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and scripting language.

Unlike the Command Prompt, PowerShell commands, or “cmdlets,” can manage the file system, the Registry, and other aspects of the Windows environment. This makes it an excellent tool for system administrators and advanced users.

In Windows 11, you can launch PowerShell using the Windows Terminal. Below is the PowerShell Core terminal.

Windows 11 Terminal PowerShell prompt
Windows 11 Terminal PowerShell prompt

You can also launch the older Windows PowerShell from the drop-down menu. Either will work for interacting with the Windows registry.

Launching Windows PowerShell
Launching Windows PowerShell

What is the Windows registry?

The registry is a hierarchical database that stores low-level settings for the Microsoft Windows operating system and applications that use the Registry. This essential part of the operating system includes keys and values, much like a standard relational database.

You can view the registry by launching the registry editor with regedit.

Windows registry editor
Windows registry editor

Accessing the Windows Registry Through PowerShell

You can use other tools from the Windows command line to access the registry, such as reg query. However, PowerShell makes the process to access specified items or a specific registry key much easier.

You can access registry keys and values using the Get-ItemProperty cmdlet. But before we start, you need to open PowerShell by searching for it in the start menu or by running ‘powershell’ in the run dialog. Once the PowerShell console is open, we can start to explore the registry.

The Get-ItemProperty cmdlet retrieves the registry key property values from the specified registry path. For instance, the path HKLM:SoftwareMicrosoftWindowsCurrentVersion refers to the Registry key in HKEY_LOCAL_MACHINESoftwareMicrosoftWindowsCurrentVersion, which contains information about the Windows setup.

For example we use the following command Get-ItemProperty to query the key path:

Get-ItemProperty -Path 'HKLM:SoftwareMicrosoftWindowsCurrentVersion'

This command would display all the properties of the specified registry path in the PowerShell console.

Using Get ItemProperty PowerShell cmdlet
Using Get ItemProperty PowerShell cmdlet

Using Get-ItemProperty and Get-ChildItem cmdlets

PowerShell provides a suite of cmdlets to interact with the registry, primarily Get-ItemProperty and Get-ChildItem.

The Get-ChildItem cmdlet is used to get the child registry keys of a particular path. It works similarly to the Get-ItemProperty cmdlet, but it returns the registry keys instead of registry values.

A practical use of the Get-ChildItem cmdlet can be seen in the following example, where we retrieve all the registry keys under the Control Panel:

Get-ChildItem -Path 'HKCU:Control Panel'
Get ChildItem PowerShell cmdlet
Get ChildItem PowerShell cmdlet

Digging Deeper: More on Get-ItemPropertyValue and Get-Item cmdlets

To get the current value of a specific registry value from a registry key, we use the Get-ItemPropertyValue cmdlet. This PowerShell command is particularly useful when retrieving a single registry value from a reg key.

You can use the command below to display the contents of a registry key, in this case “ProductName” under Current Version:

Get-ItemPropertyValue 'HKLM:SOFTWAREMicrosoftWindows NTCurrentVersion' -Name ProductName
Get ItemPropertyValue in PowerShell
Get ItemPropertyValue in PowerShell

Additionally, we have the Get-Item cmdlet. The Get-Item cmdlet is used to get the item at the specified path. This could be a file, a registry entry, or even a registry key.

Managing Registry Values Remotely

You can manage remote computers with PowerShell using the Enter-PSSession cmdlet. Using the cmdlet will allow you to establish a secure connection to a remote computer and then run PowerShell cmdlets as if you were local to the computer.

So, we can combine Enter-PSSession with the other commands we have discussed to work with a remote computer’s remote registry.

Check out my post covering Enter-PSSession here: Enter-PSsession: Run PowerShell Remote Commands.

Using Test-Path

The Test-Path cmdlet comes in handy when checking whether a registry key or registry value exists at a specified path. It returns true if the item exists at the specified path and false if it doesn’t.

For instance, to check whether a registry key exists in the current user hive, we could use the below command:

Test-Path -Path 'HKCU:Control PanelMouse'
Test Path cmdlet in PowerShell 1
Test Path cmdlet in PowerShell 1

If you are writing scripts for automation, this is a handy cmdlet that allows you to check whether or not a particular key exists and then perform other actions based on the key’s existence.

Frequently Asked Questions

Why Is the Get-ChildItem cmdlet Important in PowerShell?

The Get-ChildItem cmdlet stands as a cornerstone in PowerShell’s toolset. It enumerates registry keys within a defined registry path, extending its utility beyond registry to include directories and files within the file system. It offers a dynamic and effective way to traverse your system. Wildcards, an added feature in the Get-ChildItem command, add a new level of flexibility to the tool.

How to Ascertain the Existence of a Registry Key or Value?

PowerShell employs the Test-Path cmdlet to validate the existence of a registry key or value at a designated path. This cmdlet is efficient and provides a Boolean value in response – true if the registry key or value exists and false otherwise. This becomes an essential component while scripting automated system tasks in PowerShell.

Is It Feasible to Work with the Windows Registry on a Remote Computer?

Indeed! PowerShell’s brilliance lies in its capacity for remote operations. Merging Enter-PSSession with registry commands opens doors to extract registry keys from remote computers.

Can PowerShell Commands Output Be Simplified for Easy Reading?

PowerShell’s data output comes in a structured pattern that may sometimes be intricate to interpret. Using a pipe to direct the output to Select-Object, you can cherry-pick the properties you want to display for simpler output. The Format-Table cmdlet is another handy tool to present your data in an organized and understandable format.

Can PowerShell Create a New Key in the Windows Registry?

Certainly. The New-Item cmdlet in PowerShell creates fresh registry keys at the required path, paving the way for new system configurations or settings.

What is the Hassle-Free Method to Rename a Registry Key?

The Rename-ItemProperty cmdlet in PowerShell helps you rename an existing registry key with ease. Providing the Path, Name, and NewName parameters makes the task of modifying system configurations effortless.

How to Eliminate an Unwanted Registry Entry?

The Remove-ItemProperty cmdlet in PowerShell facilitates removing an undesirable registry entry. By stating the -Path and -Name parameters, you can eliminate any unwanted registry values, thereby aiding in the cleanup of old or outdated system configurations.

How to Get a Registry Value in Binary Data Format Using PowerShell?

Using the Get-ItemProperty cmdlet provides registry values in a format easy for humans to read. However, when working with binary data, the Format-Hex cmdlet can be used. This cmdlet reveals registry values in hexadecimal format, helpful when working with binary data.

What Role Does the Set-ItemProperty cmdlet Play in PowerShell?

The Set-ItemProperty cmdlet in PowerShell provides the capability to alter the value of a registry key. This cmdlet is crucial when it is necessary to adjust system configurations by altering registry values. A registry value at a required path can be easily set by stating the -Path, -Name, and -Value parameters.

Use PowerShell for registry automation

PowerShell’s core strength resides in its ability to manipulate registry entries like the file system. As we have seen, PowerShell has many great cmdlets to work with the Windows registry for both viewing and modifying values. The Get-ChildItem, Get-ItemProperty, and Get-ItemPropertyValue cmdlets demonstrate this power.

It serves as a robust scripting environment equipped to automate and handle intricate system tasks. Whether you’re a system administrator with the responsibility of managing multiple systems or an advanced user seeking to streamline tasks, PowerShell mastery can empower you to take absolute control of the Windows Registry and more.

Subscribe to VirtualizationHowto via Email πŸ””

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com and has over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, Brandon has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.