0
0
PowerShellscripting~5 mins

Registry operations in PowerShell

Choose your learning style9 modes available
Introduction

The Windows registry stores important settings for your computer and programs. Registry operations let you read, add, change, or remove these settings using scripts.

You want to check if a program is installed by reading its registry key.
You need to change a system setting automatically for many computers.
You want to add a new configuration for an application without opening its interface.
You want to clean up old or unused registry entries to keep the system tidy.
Syntax
PowerShell
Get-ItemProperty -Path <RegistryPath> [-Name <PropertyName>]
Set-ItemProperty -Path <RegistryPath> -Name <PropertyName> -Value <NewValue>
New-Item -Path <RegistryPath> [-Name <NewKeyName>]
Remove-Item -Path <RegistryPath> [-Recurse]

Get-ItemProperty reads values from a registry key.

Set-ItemProperty changes or adds a value inside a registry key.

New-Item creates a new registry key.

Remove-Item deletes a registry key or value.

Examples
Reads the 'Shell Folders' value from the current user's Explorer settings.
PowerShell
Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer' -Name 'Shell Folders'
Sets or creates the 'Setting1' value to 'Enabled' under 'MyApp' key.
PowerShell
Set-ItemProperty -Path 'HKCU:\Software\MyApp' -Name 'Setting1' -Value 'Enabled'
Creates a new registry key called 'NewFeature' inside 'MyApp'.
PowerShell
New-Item -Path 'HKCU:\Software\MyApp\NewFeature'
Deletes the 'OldFeature' key and all its subkeys and values.
PowerShell
Remove-Item -Path 'HKCU:\Software\MyApp\OldFeature' -Recurse
Sample Program

This script creates a new registry key under the current user, sets a value, reads it back to confirm, then deletes the key to clean up.

PowerShell
Write-Output "Creating a new registry key and setting a value..."
New-Item -Path 'HKCU:\Software\DemoApp' -ErrorAction SilentlyContinue | Out-Null
Set-ItemProperty -Path 'HKCU:\Software\DemoApp' -Name 'DemoSetting' -Value 'TestValue'
$value = Get-ItemProperty -Path 'HKCU:\Software\DemoApp' -Name 'DemoSetting'
Write-Output "Value read from registry: $($value.DemoSetting)"
Write-Output "Cleaning up by removing the DemoApp key..."
Remove-Item -Path 'HKCU:\Software\DemoApp' -Recurse
Write-Output "Done."
OutputSuccess
Important Notes

Always be careful when changing the registry. Wrong changes can cause system problems.

Use -ErrorAction SilentlyContinue to avoid errors if a key already exists.

Run PowerShell as administrator if you need to change system-wide registry keys (like HKLM).

Summary

Registry operations let you automate reading and changing Windows settings.

Use Get-ItemProperty to read, Set-ItemProperty to write values.

Create or delete keys with New-Item and Remove-Item.