PowerShell: Get, Modify, Create, and Remove Registry Keys or Parameters | Windows OS Hub (2024)

Windows OS Hub / PowerShell / PowerShell: Get, Modify, Create, and Remove Registry Keys or Parameters

The Registry Editor (regedit.exe) and the reg.exe command-line utilities aren’t the only tools to access and manage the registry in Windows. PowerShell provides a large number of tools for the administrator to interact with the registry. Using PowerShell, you can create, modify, or delete a registry key/parameters, search for the value, and connect to the registry on a remote computer.

Contents:

  • Navigate the Windows Registry Like a File System with PowerShell
  • Get a Registry Parameter Value via PowerShell
  • Changing Registry Value with PowerShell
  • How to Create a New Register Key or Parameter with PowerShell?
  • Deleting a Registry Key or Parameter
  • How to Rename a Registry Key or a Parameter?
  • Search Registry for Keyword Using PowerShell
  • Setting Registry Key Permissions with PowerShell
  • Getting a Registry Value from a Remote Computer via PowerShell

Navigate the Windows Registry Like a File System with PowerShell

Working with the registry in PowerShell is similar to working with common files on a local disk. The main difference is that in this concept the registry keys are analogous to files, and the registry parameters are the properties of these files.

Display the list of available drives on your computer:

get-psdrive

Note that among the drives (with drive letters assigned) there are special devices available through the Registry provider – HKCU (HKEY_CURRENT_USER) and HKLM (HKEY_LOCAL_MACHINE). You can browse the registry tree the same way you navigate your drives. HKLM:\ and HKCU:\ are used to access a specific registry hive.

cd HKLM:\
Dir -ErrorAction SilentlyContinue

Those, you can access the registry key and their parameters using the same PowerShell cmdlets that you use to manage files and folders.

To refer to registry keys, use cmdlets with xxx-Item:

  • Get-Item – get a registry key
  • New-Item — create a new registry key
  • Remove-Item – delete a registry key

Registry parameters should be considered as properties of the registry key (similar to file/folder properties). The xxx-ItemProperty cmdlets are used to manage registry parameters:

  • Get-ItemProperty – get the value of a registry parameter
  • Set-ItemProperty – change the value of a registry parameter
  • New-ItemProperty – create registry parameter
  • Rename-ItemProperty – rename parameter
  • Remove-ItemProperty — remove the registry parameter

You can navigate to the specific registry key (for example, to the one responsible for the settings of automatic driver updates) using one of two commands:

cd HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DriverSearching
or
Set-Location -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DriverSearching

Get a Registry Parameter Value via PowerShell

Please note that the parameters stored in the registry key are not nested objects, but a property of a specific registry key. Those any registry key can have any number of parameters.

List the contents of the current registry key using the command:

dir

Or

Get-ChildItem

The command has displayed information about the nested registry keys and their properties. But didn’t display information about the SearchOrderConfig parameter, which is a property of the current key.

Use the Get-Item cmdlet to get the parameters of the registry key:

Get-Item .
Or
Get-Item –Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DriverSearching

As you can see, the DriverSearching key has only one parameter – SearchOrderConfig with a value of 1.

To get the value of a registry key parameter, use the Get-ItemProperty cmdlet.

$DriverUpdate = Get-ItemProperty –Path ‘HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DriverSearching’
$DriverUpdate.SearchOrderConfig

We got that the value of the SearchOrderConfig parameter is 1.

Changing Registry Value with PowerShell

To change the value of the SearchOrderConfig reg parameter, use the Set-ItemProperty cmdlet:

Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DriverSearching' -Name SearchOrderConfig -Value 0

Make sure that the parameter value has changed:

Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DriverSearching' -Name SearchOrderConfig

How to Create a New Register Key or Parameter with PowerShell?

To create a new registry key, use the New-Item command. Let’s create a new key with the name NewKey:

$HKCU_Desktop= "HKCU:\Control Panel\Desktop"
New-Item –Path $HKCU_Desktop –Name NewKey

Now let’s create a new parameter in a new registry key. Suppose we need to create a new string parameter of type REG_SZ named SuperParamString and value filetmp1.txt:

New-ItemProperty -Path $HKCU_Desktop\NewKey -Name "SuperParamString" -Value ”filetmp1.txt” -PropertyType "String"

You can use the following data types for registry parameters:

  • String (REG_SZ)
  • ExpandString (REG_EXPAND_SZ)
  • MultiString (REG_MULTI_SZ)
  • Binary (REG_BINARY)
  • DWord (REG_DWORD)
  • Qword (REG_QWORD)
  • Unknown (unsupported registry data type)

Make sure that the new key and parameter have appeared in the registry.

How to check if a registry key exists?

If you need to check if a specific registry key exists, use the Test-Path cmdlet:

Test-Path 'HKCU:\Control Panel\Desktop\NewKey'

The following PowerShell script will check if a specific registry value exists, and if not, create it.

regkey='HKCU:\Control Panel\Desktop\NewKey'
$regparam='testparameter'
if (Get-ItemProperty -Path $regkey -Name $regparam -ErrorAction Ignore)
{write-host 'The registry entry already exist'}
else
{New-ItemProperty -Path $regkey -Name $regparam -Value ”woshub_test” -PropertyType "String"}

Using the Copy-Item cmdlet, you can copy entries from one registry key to another:

$source='HKLM:\SOFTWARE\7-zip\'
$dest = 'HKLM:\SOFTWARE\backup'
Copy-Item -Path $source -Destination $dest -Recurse

If you want to copy everything, including subkeys, add the –Recurse switch.

Deleting a Registry Key or Parameter

The Remove-ItemProperty command is used to remove a parameter in the registry key. Let’s remove the parameter SuperParamString created earlier:

$HKCU_Desktop= "HKCU:\Control Panel\Desktop"
Remove-ItemProperty –Path $HKCU_Desktop\NewKey –Name "SuperParamString"

You can delete the entire registry key with all its contents:

Remove-Item –Path $HKCU_Desktop\NewKey –Recurse

Note. –Recurse switch indicates that all subkeys have to be removed recursively.

To remove all items in the reg key (but not the key itself):

Remove-Item –Path $HKCU_Desktop\NewKey\* –Recurse

How to Rename a Registry Key or a Parameter?

You can rename the registry parameter with the command:

Rename-ItemProperty –path ‘HKCU:\Control Panel\Desktop\NewKey’ –name "SuperParamString" –newname “OldParamString”

In the same way, you can rename the registry key:

Rename-Item -path 'HKCU:\Control Panel\Desktop\NewKey' OldKey

Search Registry for Keyword Using PowerShell

PowerShell allows you to search the registry. The next following searches the HKCU:\Control Panel\Desktop for parameters, whose names contain the *dpi* key.

$Path = (Get-ItemProperty ‘HKCU:\Control Panel\Desktop’)
$Path.PSObject.Properties | ForEach-Object {
If($_.Name -like '*dpi*'){
Write-Host $_.Name ' = ' $_.Value
}
}

To find a registry key with a specific name:

Get-ChildItem -path HKLM:\ -recurse -ErrorAction SilentlyContinue | Where-Object {$_.Name -like "*woshub*"}

Setting Registry Key Permissions with PowerShell

You can get the current registry key permissions using the Get-ACL cmdlet.

$rights = Get-Acl -Path 'HKCU:\Control Panel\Desktop\NewKey'
$rights.Access.IdentityReference

In the following example, we will modify the ACL in this registry key to grant write access to the built-in Users group.

Get current permissions:

$rights = Get-Acl -Path 'HKCU:\Control Panel\Desktop\NewKey'

Specify the user or group you want to grant access to:

$idRef = [System.Security.Principal.NTAccount]"BuiltIn\Users"

Select access level:

$regRights = [System.Security.AccessControl.RegistryRights]::WriteKey
Set permissions inheritance settings :

$inhFlags = [System.Security.AccessControl.InheritanceFlags]::None
$prFlags = [System.Security.AccessControl.PropagationFlags]::None

Access type (Allow/Deny):

$acType = [System.Security.AccessControl.AccessControlType]::Allow
Create an access rule:

$rule = New-Object System.Security.AccessControl.RegistryAccessRule ($idRef, $regRights, $inhFlags, $prFlags, $acType)

Add a new rule to the current ACL:

$rights.AddAccessRule($rule)

Apply new permissions to the registry key:

$rights | Set-Acl -Path 'HKCU:\Control Panel\Desktop\NewKey'

Make sure the new group appears in the ACL of the registry key.

Getting a Registry Value from a Remote Computer via PowerShell

PowerShell allows you to access the registry of a remote computer. You can connect to a remote computer either using WinRM (Invoke-Command or Enter-PSSession). To get the value of a registry parameter from a remote computer:

Invoke-Command –ComputerName srv-fs1 –ScriptBlock {Get-ItemProperty -Path 'HKLM:\System\Setup' -Name WorkingDirectory}

Or using a remote registry connection (the RemoteRegistry service must be enabled)

$Server = "lon-fs1"
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Server)
$RegKey= $Reg.OpenSubKey("System\Setup")
$RegValue = $RegKey.GetValue("WorkingDirectory")

Tip. If you have to create/modify a certain registry parameter on multiple domain computers, it is easier to use GPO features.

So we’ve covered typical examples of using PowerShell to access and manage Windows registry entries. You can use them in your automation scripts.

PowerShell: Get, Modify, Create, and Remove Registry Keys or Parameters | Windows OS Hub (2024)
Top Articles
Liquidation Level: What It Is, How It Works
Behavioral economics, explained
Www.paystubportal.com/7-11 Login
O'reilly's Auto Parts Closest To My Location
Cottonwood Vet Ottawa Ks
Coverage of the introduction of the Water (Special Measures) Bill
Air Canada bullish about its prospects as recovery gains steam
Red Wing Care Guide | Fat Buddha Store
What is IXL and How Does it Work?
The Wicked Lady | Rotten Tomatoes
Alaska: Lockruf der Wildnis
735 Reeds Avenue 737 & 739 Reeds Ave., Red Bluff, CA 96080 - MLS# 20240686 | CENTURY 21
Diesel Mechanic Jobs Near Me Hiring
Tcgplayer Store
iOS 18 Hadir, Tapi Mana Fitur AI Apple?
Interactive Maps: States where guns are sold online most
Odfl4Us Driver Login
Walgreens Tanque Verde And Catalina Hwy
Ac-15 Gungeon
Surplus property Definition: 397 Samples | Law Insider
How Taraswrld Leaks Exposed the Dark Side of TikTok Fame
Makemv Splunk
Bj타리
Gen 50 Kjv
Infinite Campus Asd20
Jamielizzz Leaked
Fastpitch Softball Pitching Tips for Beginners Part 1 | STACK
Kristen Hanby Sister Name
Smartfind Express Henrico
Tas Restaurant Fall River Ma
Aliciabibs
Boggle BrainBusters: Find 7 States | BOOMER Magazine
Maxpreps Field Hockey
Nancy Pazelt Obituary
Registrar Lls
Letter of Credit: What It Is, Examples, and How One Is Used
511Pa
Craigslist en Santa Cruz, California: Tu Guía Definitiva para Comprar, Vender e Intercambiar - First Republic Craigslist
Achieving and Maintaining 10% Body Fat
Unblocked Games Gun Games
Gregory (Five Nights at Freddy's)
R: Getting Help with R
Rescare Training Online
Frequently Asked Questions
Gt500 Forums
Phone Store On 91St Brown Deer
Minecraft: Piglin Trade List (What Can You Get & How)
Rovert Wrestling
Sams La Habra Gas Price
When Is The First Cold Front In Florida 2022
Latest Posts
Article information

Author: Allyn Kozey

Last Updated:

Views: 5975

Rating: 4.2 / 5 (43 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Allyn Kozey

Birthday: 1993-12-21

Address: Suite 454 40343 Larson Union, Port Melia, TX 16164

Phone: +2456904400762

Job: Investor Administrator

Hobby: Sketching, Puzzles, Pet, Mountaineering, Skydiving, Dowsing, Sports

Introduction: My name is Allyn Kozey, I am a outstanding, colorful, adventurous, encouraging, zealous, tender, helpful person who loves writing and wants to share my knowledge and understanding with you.