Working with registry entries - PowerShell (2024)

  • Article

This sample only applies to Windows platforms.

Because registry entries are properties of keys and, as such, can't be directly browsed, we need totake a slightly different approach when working with them.

Listing registry entries

There are many different ways to examine registry entries. The simplest way is to get the propertynames associated with a key. For example, to see the names of the entries in the registry keyHKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion, use Get-Item. Registry keys have aproperty with the generic name of "Property" that's a list of registry entries in the key. Thefollowing command selects the Property property and expands the items so that they're displayed in alist:

Get-Item -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion | Select-Object -ExpandProperty Property
DevicePathMediaPathUnexpandedProgramFilesDirCommonFilesDirProductId

To view the registry entries in a more readable form, use Get-ItemProperty:

Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion
ProgramFilesDir : C:\Program FilesCommonFilesDir : C:\Program Files\Common FilesProgramFilesDir (x86) : C:\Program Files (x86)CommonFilesDir (x86) : C:\Program Files (x86)\Common FilesCommonW6432Dir : C:\Program Files\Common FilesDevicePath : C:\WINDOWS\infMediaPathUnexpanded : C:\WINDOWS\MediaProgramFilesPath : C:\Program FilesProgramW6432Dir : C:\Program FilesSM_ConfigureProgramsName : Set Program Access and DefaultsSM_GamesName : GamesPSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWA RE\Microsoft\Windows\CurrentVersionPSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWA RE\Microsoft\WindowsPSChildName : CurrentVersionPSDrive : HKLMPSProvider : Microsoft.PowerShell.Core\Registry

The Windows PowerShell-related properties for the key are all prefixed with "PS", such asPSPath, PSParentPath, PSChildName, and PSProvider.

You can use the *.* notation for referring to the current location. You can use Set-Location tochange to the CurrentVersion registry container first:

Set-Location -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion

Alternatively, you can use the built-in HKLM: PSDrive with Set-Location:

Set-Location -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion

You can then use the . notation for the current location to list the properties withoutspecifying a full path:

Get-ItemProperty -Path .
...DevicePath : C:\WINDOWS\infMediaPathUnexpanded : C:\WINDOWS\MediaProgramFilesDir : C:\Program Files...

Path expansion works the same as it does within the filesystem, so from this location you can getthe ItemProperty listing for HKLM:\SOFTWARE\Microsoft\Windows\Help usingGet-ItemProperty -Path ..\Help.

Getting a single registry entry

If you want to retrieve a specific entry in a registry key, you can use one of several possibleapproaches. This example finds the value of DevicePath inHKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion.

Using Get-ItemProperty, use the Path parameter to specify the name of the key, and theName parameter to specify the name of the DevicePath entry.

Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion -Name DevicePath
DevicePath : C:\WINDOWS\infPSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersionPSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\WindowsPSChildName : CurrentVersionPSDrive : HKLMPSProvider : Microsoft.PowerShell.Core\Registry

This command returns the standard Windows PowerShell properties as well as the DevicePathproperty.

Note

Although Get-ItemProperty has Filter, Include, and Exclude parameters, they can'tbe used to filter by property name. These parameters refer to registry keys, which are itempaths and not registry entries, which are item properties.

Another option is to use the reg.exe command line tool. For help with reg.exe, type reg.exe /?at a command prompt. To find the DevicePath entry, use reg.exe as shown in the followingcommand:

reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion /v DevicePath
! REG.EXE VERSION 3.0HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion DevicePath REG_EXPAND_SZ %SystemRoot%\inf

You can also use the WshShell COM object to find some registry entries, although this methoddoesn't work with large binary data or with registry entry names that include characters such asbackslash (\). Append the property name to the item path with a \ separator:

(New-Object -ComObject WScript.Shell).RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DevicePath")
%SystemRoot%\inf

Setting a single registry entry

If you want to change a specific entry in a registry key, you can use one of several possibleapproaches. This example modifies the Path entry under HKEY_CURRENT_USER\Environment. ThePath entry specifies where to find executable files.

  1. Retrieve the current value of the Path entry using Get-ItemProperty.
  2. Add the new value, separating it with a ;.
  3. Use Set-ItemProperty with the specified key, entry name, and value to modify the registryentry.
$value = Get-ItemProperty -Path HKCU:\Environment -Name Path$newpath = $value.Path += ";C:\src\bin\"Set-ItemProperty -Path HKCU:\Environment -Name Path -Value $newpath

Note

Although Set-ItemProperty has Filter, Include, and Exclude parameters, theycan't be used to filter by property name. These parameters refer to registry keys—which are itempaths—and not registry entries—which are item properties.

Another option is to use the Reg.exe command line tool. For help with reg.exe, type reg.exe /? ata command prompt.

The following example changes the Path entry by removing the path added in the example above.Get-ItemProperty is still used to retrieve the current value to avoid having to parse the stringreturned from reg query. The SubString and LastIndexOf methods are used to retrieve thelast path added to the Path entry.

$value = Get-ItemProperty -Path HKCU:\Environment -Name Path$newpath = $value.Path.SubString(0, $value.Path.LastIndexOf(';'))reg add HKCU\Environment /v Path /d $newpath /f
The operation completed successfully.

Creating new registry entries

To add a new entry named "PowerShellPath" to the CurrentVersion key, use New-ItemProperty withthe path to the key, the entry name, and the value of the entry. For this example, we will take thevalue of the Windows PowerShell variable $PSHome, which stores the path to the installationdirectory for Windows PowerShell.

You can add the new entry to the key using the following command, and the command also returnsinformation about the new entry:

New-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion -Name PowerShellPath -PropertyType String -Value $PSHome
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersionPSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsPSChildName : CurrentVersionPSDrive : HKLMPSProvider : Microsoft.PowerShell.Core\RegistryPowerShellPath : C:\Program Files\Windows PowerShell\v1.0

The PropertyType must be the name of a Microsoft.Win32.RegistryValueKind enumeration memberfrom the following table:

PropertyType ValueMeaning
BinaryBinary data
DWordA number that's a valid UInt32
ExpandStringA string that can contain environment variables that are dynamically expanded
MultiStringA multiline string
StringAny string value
QWord8 bytes of binary data

You can add a registry entry to multiple locations by specifying an array of values for the Pathparameter:

New-ItemProperty -Name PowerShellPath -PropertyType String -Value $PSHome ` -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion, HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion

You can also overwrite a pre-existing registry entry value by adding the Force parameter to anyNew-ItemProperty command.

Renaming registry entries

To rename the PowerShellPath entry to "PSHome," use Rename-ItemProperty:

Rename-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion -Name PowerShellPath -NewName PSHome

To display the renamed value, add the PassThru parameter to the command.

Rename-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion -Name PowerShellPath -NewName PSHome -passthru

Deleting registry entries

To delete both the PSHome and PowerShellPath registry entries, use Remove-ItemProperty:

Remove-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion -Name PSHomeRemove-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion -Name PowerShellPath
Working with registry entries - PowerShell (2024)

FAQs

Working with registry entries - PowerShell? ›

It is easy to change add registry keys and values. You can use the New-Item cmdlet to create any key in any registry hive. Once you create the key, you can use New-ItemProperty to set a registry value entry.

How to add registry entry using PowerShell? ›

It is easy to change add registry keys and values. You can use the New-Item cmdlet to create any key in any registry hive. Once you create the key, you can use New-ItemProperty to set a registry value entry.

How to read registry in 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.

How do I clean up the registry in PowerShell? ›

To delete the registry key using PowerShell, we can use the Remove-Item command. Remove-Item command removes the registry key from the path specified. For example, we have the registry key name NodeSoftware stored at the path HKLM, under the Software key.

How to list down all registry keys using PowerShell? ›

Use the Get-Item cmdlet to retrieve the properties of the registry key. Pipe the registry properties through the ForEach-Object cmdlet. In the script block of the ForEach-Object cmdlet, use the Get-ItemProperty cmdlet to retrieve the property values.

How to modify a registry value using PowerShell? ›

Updating the Value of a Registry Key Using PowerShell

PowerShell provides the Set-ItemProperty cmdlet to change the value of a specific registry key. By specifying the path of the key, the name of the value, and the new value data, PowerShell will update the value accordingly.

How do I access registry entries? ›

In the search box on the taskbar, type regedit, then select Registry Editor (Desktop app) from the results. Right-click Start , then select Run. Type regedit in the Open: box, and then select OK.

How to check if registry entry 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. This script retrieves the value of the Connected registry key from the specified path, and then checks if the value is equal to "1".

What are the types of registry values in PowerShell? ›

The possible types for a registry value are: string (REG_SZ or REG_MULTI_SZ), expandable string (REG_EXPAND_SZ), integer (REG_DWORD) and binary (REG_BINARY). In order to define a REG_MULTI_SZ value, enter the strings that compose that value, one per-line.

What are the registry paths in PowerShell? ›

The PowerShell Registry provider exposes two registry paths: HKLM for HKEY_LOCAL_MACHINE and HKCU for HKEY_CURRENT_USER. The Get-PSDrive cmdlet gets the drives available in the current session, including logical mapped network drives and drives exposed by Windows PowerShell providers.

How do I clean my registry entries? ›

Type "regedit" into the search dialog in the Windows 10 taskbar, and then click the Registry Editor app that appears in the search results. The Windows Registry Editor will then open, allowing you to review, change or delete registry entries (Figure 1).

How to remove value from registry in PowerShell? ›

Management) - 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.

How to create a folder in registry using PowerShell? ›

Only the steps:
  1. Store the current working location by using the Push-Location cmdlet.
  2. Change the current working location to the appropriate registry drive by using the Set-Location cmdlet.
  3. Use the Test-Path cmdlet to determine if the registry key already exists.
  4. Use the New-Item cmdlet to create the new registry key.
May 9, 2012

How to read a registry key with PowerShell? ›

Getting a single registry entry

Using Get-ItemProperty , use the Path parameter to specify the name of the key, and the Name parameter to specify the name of the DevicePath entry. This command returns the standard Windows PowerShell properties as well as the DevicePath property.

How do I open the registry editor in PowerShell? ›

Open Command Prompt or PowerShell: Press Windows Key + X to open the Power User menu, then select Command Prompt or PowerShell from the list. Type “regedit”: In the Command Prompt or PowerShell window, simply type “regedit” (without quotes) and press Enter.

What is the PowerShell function set registry key? ›

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 do I add an entry to the registry? ›

To add a new registry key using Registry Editor, take the following steps:
  1. Click the search box in the taskbar and enter regedit and then press Enter.
  2. On the left-hand side of Registry Editor, right-click the registry key or its sub-key(s).
  3. In the context menu, select New | Key as shown in Figure 4.24:

How do you add an entry to a list in PowerShell? ›

You can use the += operator to add an element to an array. The following example shows how to add an element to the $a array. When you use the += operator, PowerShell actually creates a new array with the values of the original array and the added value.

How do I add a host file entry in PowerShell? ›

To add the content to the host file, we need to first retrieve the content using the Get-Content command and the need to set the content to the host file after adding the entry. The Code is shown below. We need to add the global entry to it.

How do I import registry entries? ›

Restore a manual back up
  1. Select Start , type regedit.exe, and then press Enter. ...
  2. In Registry Editor, click File > Import.
  3. In the Import Registry File dialog box, select the location to which you saved the backup copy, select the backup file, and then click Open.

Top Articles
Here comes another Netflix price hike
5 reasons to switch from an Android phone to an Apple iPhone
Shs Games 1V1 Lol
Hawkeye 2021 123Movies
Dr Doe's Chemistry Quiz Answer Key
Sportsman Warehouse Cda
Vanadium Conan Exiles
Craigslist Dog Sitter
Tugboat Information
Bubbles Hair Salon Woodbridge Va
Nalley Tartar Sauce
Walmart End Table Lamps
Pac Man Deviantart
Destiny 2 Salvage Activity (How to Complete, Rewards & Mission)
Sonic Fan Games Hq
Lowe's Garden Fence Roll
Ibukunore
623-250-6295
Jbf Wichita Falls
Graphic Look Inside Jeffrey Dahmer
Tripadvisor Napa Restaurants
Doki The Banker
Lacey Costco Gas Price
Cylinder Head Bolt Torque Values
Possum Exam Fallout 76
How Do Netspend Cards Work?
Best New England Boarding Schools
Swgoh Boba Fett Counter
Grandstand 13 Fenway
ShadowCat - Forestry Mulching, Land Clearing, Bush Hog, Brush, Bobcat - farm & garden services - craigslist
Smartfind Express Henrico
Skip The Games Ventura
Case Funeral Home Obituaries
Shih Tzu dogs for sale in Ireland
When His Eyes Opened Chapter 2048
10 games with New Game Plus modes so good you simply have to play them twice
Dying Light Nexus
Tillman Funeral Home Tallahassee
Kornerstone Funeral Tulia
Gun Mayhem Watchdocumentaries
Bekah Birdsall Measurements
Tattoo Shops In Ocean City Nj
Citibank Branch Locations In North Carolina
How I Passed the AZ-900 Microsoft Azure Fundamentals Exam
Collision Masters Fairbanks
Ohio Road Construction Map
Server Jobs Near
Pronósticos Gulfstream Park Nicoletti
Jovan Pulitzer Telegram
Gameplay Clarkston
Latest Posts
Article information

Author: Clemencia Bogisich Ret

Last Updated:

Views: 6197

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Clemencia Bogisich Ret

Birthday: 2001-07-17

Address: Suite 794 53887 Geri Spring, West Cristentown, KY 54855

Phone: +5934435460663

Job: Central Hospitality Director

Hobby: Yoga, Electronics, Rafting, Lockpicking, Inline skating, Puzzles, scrapbook

Introduction: My name is Clemencia Bogisich Ret, I am a super, outstanding, graceful, friendly, vast, comfortable, agreeable person who loves writing and wants to share my knowledge and understanding with you.