The Windows registry stores important settings for your computer and programs. Registry operations let you read, add, change, or remove these settings using scripts.
Registry operations in 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.
Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer' -Name 'Shell Folders'
Set-ItemProperty -Path 'HKCU:\Software\MyApp' -Name 'Setting1' -Value 'Enabled'
New-Item -Path 'HKCU:\Software\MyApp\NewFeature'Remove-Item -Path 'HKCU:\Software\MyApp\OldFeature' -RecurseThis 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.
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."
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).
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.
Practice
Solution
Step 1: Understand cmdlet purposes
Get-ItemProperty reads registry values, Set-ItemProperty writes values, New-Item creates keys, Remove-Item deletes keys.Step 2: Identify reading operation
Since the question asks about reading, Get-ItemProperty is the correct cmdlet.Final Answer:
Get-ItemProperty -> Option DQuick Check:
Read registry value = Get-ItemProperty [OK]
- Confusing Set-ItemProperty as reading cmdlet
- Using New-Item or Remove-Item which manage keys, not values
- Trying to read with Remove-Item
TestValue to 123 under HKCU:\Software\MyApp?Solution
Step 1: Identify correct cmdlet and parameters
Set-ItemProperty sets a registry value. The parameters are -Path for key, -Name for value name, and -Value for the data.Step 2: Check parameter order and names
Set-ItemProperty -Path 'HKCU:\Software\MyApp' -Name 'TestValue' -Value 123 correctly uses -Path, -Name, and -Value in proper order. Other options mix parameters or use wrong cmdlets.Final Answer:
Set-ItemProperty -Path 'HKCU:\Software\MyApp' -Name 'TestValue' -Value 123 -> Option CQuick Check:
Set-ItemProperty syntax = Set-ItemProperty -Path 'HKCU:\Software\MyApp' -Name 'TestValue' -Value 123 [OK]
- Swapping -Name and -Value parameters
- Using Get-ItemProperty to set values
- Using New-Item which creates keys, not values
Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer' -Name 'ShellState'
Solution
Step 1: Understand Get-ItemProperty behavior
This cmdlet reads the value of a property from a registry key.Step 2: Analyze the command
The command reads the 'ShellState' value from the given registry path and outputs it.Final Answer:
Displays the value of 'ShellState' property from the specified registry key -> Option BQuick Check:
Get-ItemProperty reads registry values [OK]
- Thinking it creates or deletes keys
- Confusing property name with cmdlet name
- Expecting an error for valid property
Remove-Item -Path 'HKCU:\Software\MyApp'
But it fails with an error saying the key is not empty. How can you fix this?
Solution
Step 1: Understand Remove-Item behavior
Remove-Item cannot delete a key if it has subkeys unless -Recurse is used.Step 2: Apply -Recurse parameter
Adding -Recurse deletes the key and all its subkeys, fixing the error.Final Answer:
Add the parameter -Recurse to delete all subkeys -> Option AQuick Check:
Remove-Item -Recurse deletes key with subkeys [OK]
- Using Remove-ItemProperty which deletes values, not keys
- Trying to recreate key before deleting
- Changing registry hive path without reason
HKCU:\Software\MyApp\Settings only if it does not exist. Which script snippet correctly does this?Solution
Step 1: Check if key exists using Test-Path
Test-Path returns true if the registry key exists, so -not negates it to check non-existence.Step 2: Create key only if missing
New-Item creates the 'Settings' key under 'MyApp' only if it does not exist, avoiding errors.Final Answer:
if (-not (Test-Path 'HKCU:\Software\MyApp\Settings')) { New-Item -Path 'HKCU:\Software\MyApp' -Name 'Settings' } -> Option AQuick Check:
Use Test-Path before New-Item to avoid duplicates [OK]
- Using New-Item with -Force creates or overwrites without check
- Using Set-ItemProperty to create keys (it sets values)
- Deleting key instead of creating it
Ready to prove it?
Pass the test and this pattern moves unknown
