Working with registry keys - PowerShell (2024)

  • Article

This sample only applies to Windows platforms.

Because registry keys are items on PowerShell drives, working with them is very similar to workingwith files and folders. One critical difference is that every item on a registry-based PowerShelldrive is a container, just like a folder on a file system drive. However, registry entries and theirassociated values are properties of the items, not distinct items.

Listing all subkeys of a registry key

You can show all items directly within a registry key using Get-ChildItem. Add the optionalForce parameter to display hidden or system items. For example, this command displays the itemsdirectly within PowerShell drive HKCU:, which corresponds to the HKEY_CURRENT_USER registryhive:

Get-ChildItem -Path HKCU:\ | Select-Object Name
 Hive: Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USERName----HKEY_CURRENT_USER\AppEventsHKEY_CURRENT_USER\ConsoleHKEY_CURRENT_USER\Control PanelHKEY_CURRENT_USER\DirectShowHKEY_CURRENT_USER\dummyHKEY_CURRENT_USER\EnvironmentHKEY_CURRENT_USER\EUDCHKEY_CURRENT_USER\Keyboard LayoutHKEY_CURRENT_USER\MediaFoundationHKEY_CURRENT_USER\MicrosoftHKEY_CURRENT_USER\NetworkHKEY_CURRENT_USER\PrintersHKEY_CURRENT_USER\SoftwareHKEY_CURRENT_USER\SystemHKEY_CURRENT_USER\UninstallHKEY_CURRENT_USER\WXPHKEY_CURRENT_USER\Volatile Environment

These are the top-level keys visible under HKEY_CURRENT_USER in the Registry Editor(regedit.exe).

You can also specify this registry path by specifying the registry provider's name, followed by::. The registry provider's full name is Microsoft.PowerShell.Core\Registry, but this can beshortened to just Registry. Any of the following commands will list the contents directly underHKCU:.

Get-ChildItem -Path Registry::HKEY_CURRENT_USERGet-ChildItem -Path Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USERGet-ChildItem -Path Registry::HKCUGet-ChildItem -Path Microsoft.PowerShell.Core\Registry::HKCUGet-ChildItem HKCU:

These commands list only the directly contained items, much like using DIR in cmd.exe or lsin a UNIX shell. To show contained items, you need to specify the Recurse parameter. To list allregistry keys in HKCU:, use the following command.

Get-ChildItem -Path HKCU:\ -Recurse

Get-ChildItem can perform complex filtering capabilities through its Path, Filter,Include, and Exclude parameters, but those parameters are typically based only on name. Youcan perform complex filtering based on other properties of items using the Where-Object cmdlet.The following command finds all keys within HKCU:\Software that have no more than one subkey andalso have exactly four values:

Get-ChildItem -Path HKCU:\Software -Recurse | Where-Object {($_.SubKeyCount -le 1) -and ($_.ValueCount -eq 4) }

Copying keys

Copying is done with Copy-Item. The following example copies the CurrentVersion subkey ofHKLM:\SOFTWARE\Microsoft\Windows\ and all of its properties to HKCU:\.

Copy-Item -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion' -Destination HKCU:

If you examine this new key in the registry editor or using Get-ChildItem, you notice that youdon't have copies of the contained subkeys in the new location. In order to copy all of the contentsof a container, you need to specify the Recurse parameter. To make the preceding copy commandrecursive, you would use this command:

Copy-Item -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion' -Destination HKCU: -Recurse

You can still use other tools you already have available to perform filesystem copies. Any registryediting tools—including reg.exe, regini.exe, regedit.exe, and COM objects that supportregistry editing, such as WScript.Shell and WMI's StdRegProv class can be used from withinPowerShell.

Creating keys

Creating new keys in the registry is simpler than creating a new item in a file system. Because allregistry keys are containers, you don't need to specify the item type. Just provide an explicitpath, such as:

New-Item -Path HKCU:\Software_DeleteMe

You can also use a provider-based path to specify a key:

New-Item -Path Registry::HKCU\Software_DeleteMe

Deleting keys

Deleting items is essentially the same for all providers. The following commands silently removeitems:

Remove-Item -Path HKCU:\Software_DeleteMeRemove-Item -Path 'HKCU:\key with spaces in the name'

Removing all keys under a specific key

You can remove contained items using Remove-Item, but you will be prompted to confirm the removalif the item contains anything else. For example, if we attempt to delete the HKCU:\CurrentVersionsubkey we created, we see this:

Remove-Item -Path HKCU:\CurrentVersion
ConfirmThe item at HKCU:\CurrentVersion\AdminDebug has children and the -recurseparameter was not specified. If you continue, all children will be removed withthe item. Are you sure you want to continue?[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):

To delete contained items without prompting, specify the Recurse parameter:

Remove-Item -Path HKCU:\CurrentVersion -Recurse

If you wanted to remove all items within HKCU:\CurrentVersion but not HKCU:\CurrentVersionitself, you could instead use:

Remove-Item -Path HKCU:\CurrentVersion\* -Recurse
Working with registry keys - PowerShell (2024)

FAQs

How to read registry keys with PowerShell? ›

For registry operations, use:
  1. Get-ItemProperty and Get-ItemPropertyValue to read registry values and data.
  2. Get-Item to get registry keys and sub-keys (but not to read registry values and data)
  3. Get-ChildItem to list sub-keys within keys and hives.
Aug 24, 2016

How to edit registry key using PowerShell? ›

If we know that the registry key value already exists and we want to modify the value, we can use the `Set-ItemProperty` cmdlet. Think of each Registry Value Entry as an attribute of a particular registry key.

How to use registry keys? ›

Follow these steps:
  1. Open Registry Editor: Press Windows Key + R, type “regedit”, and press Enter.
  2. Locate the Key to Edit: Navigate to the specific key you wish to edit.
  3. Double-Click the Key or Value: Double-click the key or value you want to edit.
  4. Make the Necessary Changes: ...
  5. Save the Changes:
Nov 6, 2023

How to import registry key using PowerShell script? ›

reg file, you can use the reg import command in the Command Prompt or PowerShell:
  1. Open an elevated Command Prompt or PowerShell (Run as Administrator).
  2. Import the . reg file with the reg import command.

How do I navigate registry keys? ›

To locate a Reg Key, expand the first folder in that Reg Key's name (example: HKEY_LOCAL_MACHINE) by clicking the right-facing arrow to the left of that folder. 4. Continue expanding folders to locate the Reg Key you need to create, edit, or delete.

What PowerShell command will list only the contents of a registry key? ›

PowerShell. Core\Registry::HKCU Get-ChildItem HKCU: These commands list only the directly contained items, much like using DIR in cmd.exe or ls in a UNIX shell.

How to check if a registry key exists in PowerShell? ›

You can use the Get-ItemProperty cmdlet to retrieve the value of a registry key, and then use an if statement to check the value.

What is the set registry key function in PowerShell? ›

The `Set-RegistryKeyValue` function sets the value of a registry key. If the key doesn't exist, it is created first. Uses PowerShell's `New-ItemPropery` to create the value if doesn't exist. Otherwise uses `Set-ItemProperty` to set the value.

How to remove value from registry key in PowerShell? ›

The Remove-ItemProperty cmdlet deletes a property and its value from an item. You can use it to delete registry values and the data that they store.

What are the 5 registry keys? ›

What are the five registry keys? In most versions of Windows, the following keys are in the registry: HKEY_CLASSES_ROOT (HKCR), HKEY_CURRENT_USER (HKCU), HKEY_LOCAL_MACHINE (HKLM), HKEY_USERS (HKU), and HKEY_CURRENT_CONFIG.

What is the purpose of the registry key? ›

Registry keys are containers that act like folders, with values or subkeys contained within them. Registry values are similar to files (not containers). The relatively straightforward syntax and simple user interface keep the size of the registry low. Not all applications use the registry.

How do I grant access to registry keys? ›

To open the Registry Editor, click Start > Run > Type regedit.exe > Press Enter. In the left pane, right-click on the key that needs permission then click Permissions. Select the group or username where the permission needs to be applied. Select the Allow check box for the access levels of the group or username.

How to read a registry key with PowerShell? ›

To browse through the registry in PowerShell, we can use the Get-ChildItem command. For example to get all keys from the path HKLM:\Hardware we can use the below command. Or you can set the location and use the dir (get-ChildItem or ls) command to browse the path.

Can you use PowerShell to edit registry? ›

PowerShell provides a set of cmdlets, or commands, for working with the Registry, which makes it easy to retrieve, create, and modify Registry values. PowerShell also allows for remote registry management, making it an invaluable tool for administrators managing multiple systems.

How to open registry keys using run command? ›

Right-click Start , then select Run. Type regedit in the Open: box, and then select OK.

How to read a registry key? ›

Reading from the registry is done via the REG QUERY command. This command can be used to retrieve values of any key from within the registry.

How to read key in PowerShell? ›

You can use the [System. Console]::ReadKey() method to find the name of the key you pressed.

How do I open the regedit path in PowerShell? ›

To do this, we'll need the name of the current PowerShell path ( Get-Location ), convert it to the format that Regedit saves it as ( Convert-Path with "Computer\" prefixed to it), update the "LastKey" key/value in the registry ( New-ItemProperty ), and then open RegEdit ( Start-Process ).

Top Articles
Writing a Job Description Summary - Human Resource Services
About TravelInsurance.com
Where To Go After Howling Pit Code Vein
Skyward Sinton
Craglist Oc
Caroline Cps.powerschool.com
Hotels Near 500 W Sunshine St Springfield Mo 65807
Flights to Miami (MIA)
Mawal Gameroom Download
Evita Role Wsj Crossword Clue
Joe Gorga Zodiac Sign
Www.paystubportal.com/7-11 Login
Keurig Refillable Pods Walmart
Hope Swinimer Net Worth
About Us | TQL Careers
Mini Handy 2024: Die besten Mini Smartphones | Purdroid.de
2015 Honda Fit EX-L for sale - Seattle, WA - craigslist
Best Suv In 2010
Unlv Mid Semester Classes
24 Hour Drive Thru Car Wash Near Me
2020 Military Pay Charts – Officer & Enlisted Pay Scales (3.1% Raise)
Lehmann's Power Equipment
Royal Cuts Kentlands
Teacup Yorkie For Sale Up To $400 In South Carolina
Lisas Stamp Studio
Understanding Gestalt Principles: Definition and Examples
Drift Hunters - Play Unblocked Game Online
Barista Breast Expansion
Villano Antillano Desnuda
Ts Modesto
Does Royal Honey Work For Erectile Dysfunction - SCOBES-AR
Ff14 Sage Stat Priority
Shauna's Art Studio Laurel Mississippi
Magicseaweed Capitola
10 games with New Game Plus modes so good you simply have to play them twice
Labyrinth enchantment | PoE Wiki
„Wir sind gut positioniert“
Google Flights Orlando
Exploring the Digital Marketplace: A Guide to Craigslist Miami
Craigslist Minneapolis Com
Sound Of Freedom Showtimes Near Amc Mountainside 10
Rocket Lab hiring Integration & Test Engineer I/II in Long Beach, CA | LinkedIn
Sherwin Source Intranet
Iron Drop Cafe
300+ Unique Hair Salon Names 2024
Rocket Bot Royale Unblocked Games 66
Minecraft Enchantment Calculator - calculattor.com
The Missile Is Eepy Origin
Ff14 Palebloom Kudzu Cloth
211475039
The Love Life Of Kelsey Asbille: A Comprehensive Guide To Her Relationships
Latest Posts
Article information

Author: Saturnina Altenwerth DVM

Last Updated:

Views: 6525

Rating: 4.3 / 5 (44 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Saturnina Altenwerth DVM

Birthday: 1992-08-21

Address: Apt. 237 662 Haag Mills, East Verenaport, MO 57071-5493

Phone: +331850833384

Job: District Real-Estate Architect

Hobby: Skateboarding, Taxidermy, Air sports, Painting, Knife making, Letterboxing, Inline skating

Introduction: My name is Saturnina Altenwerth DVM, I am a witty, perfect, combative, beautiful, determined, fancy, determined person who loves writing and wants to share my knowledge and understanding with you.