Category filter
Scripts to manage registry entries on Windows devices
The Windows registry is organized into a tree-like structure consisting of nodes called registry keys. These registry keys contain values called registry entries, which are properties of the corresponding registry key. You can customize and configure the system to suit your specific needs by managing registry entries on Windows devices.
While it is possible to modify the registry manually using the built-in Registry Editor tool, it can be time-consuming and error prone. This document focuses on scripts designed to manage registry entries efficiently. Use Hexnode’s Execute Custom Script feature to remotely deploy these scripts to multiple devices in no time.
PowerShell commands to manage registry entries in local machine registry
List the registry entries
The easiest way to examine registry entries is to get the name of the property associated with the key. A registry key has a property with the generic name “Property”, which is a list of registry entries in the key. The following command selects the Property attribute and expands the items so that they appear in the list:
1 2 |
Get-Item -Path "Registry_path" | Select-Object -ExpandProperty Property |
For example, to display the list of entries in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control use the following script:
1 2 |
Get-Item -Path Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control | Select-Object -ExpandProperty Property |
For a more readable representation of the registry entries, you can use the Get-ItemProperty command.
1 |
Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control |
You can navigate to the Control registry container using the Set-Location command. And, using the “.” notation for the current location helps list the properties without defining the full path.
1 2 |
Set-Location -Path HKLM:\SYSTEM\CurrentControlSet\Control Get-ItemProperty -Path . |
Retrieve a single registry entry
Use the following command to retrieve a specific registry entry CurrentUser from the registry HKLM:\SYSTEM\CurrentControlSet\Control.
1 |
Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control -Name CurrentUser |
Create a new registry entry
To add a new item named “PowerShellPath” to the Control key, the New-ItemProperty command can be used with the key’s path, the entry name, and its corresponding value. In this example, it takes the value of the Windows PowerShell variable $PSHome, which stores the installation directory path for the program.
1 |
New-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control -Name PowerShellPath -PropertyType String -Value $PSHome |
To add a registry entry to multiple locations, you can provide an array of values for the Path parameter. For example:
1 2 |
New-ItemProperty -Name PowerShellPath -PropertyType String -Value $PSHome ` -Path HKLM:\SYSTEM\CurrentControlSet\Control, HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion |
Use Force parameter with any New-ItemProperty command to overwrite a pre-existing registry entry value.
To set the PropertyType, it is necessary to use the name of a member from the Microsoft.Win32.RegistryValueKind enumeration presented in the table below:
PropertyType Value | Meaning |
---|---|
Binary | Binary data |
DWord | A number that’s a valid UInt32 |
ExpandString | A string that can contain environment variables that are dynamically expanded. |
MultiString | A multiline string |
String | Any string value |
QWord | 8 bytes of binary data |
Rename a registry entry
Use the following command to rename the PowerShellPath entry to “PSHome”.
1 |
Rename-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control -Name PowerShellPath -NewName PSHome -passthru |
Delete the registry entries
Use the following command to delete PSHome registry entry.
1 |
Remove-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Control -Name PSHome |