PowerShell: Windows Registry

By Xah Lee. Date: . Last updated: .

Go to a registry key location

# goto Registry HKEY_CURRENT_USER
cd hkcu:
# goto Registry HKEY_LOCAL_MACHINE
cd hklm:
# goto a key directly
cd hkcu:\Software\Microsoft\Windows\CurrentVersion\Explorer\Wallpapers\

List keys in current location

dir

List values in current location

Use Get-ItemProperty (alias gp)

gp .
PowerShell show registry values 20210607214557
show registry values by Get-ItemProperty

Set value

Use Set-ItemProperty (alias sp).

example of set a registry value:

# 2025-02-03
# set desktop background image

# Path to your image
$myimg = "D:/xah_ddrive_2022/xah_photos/xah_food/xah_food_IMG_2024-08-19_0431-s40.JPG"

# Set the wallpaper in the registry
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name Wallpaper -Value $myimg

# Update the wallpaper style.
# 0: Center the wallpaper
# 1: Tile the wallpaper
# 2: Stretch the wallpaper
# 3: Fit the wallpaper
# 4: Fill the wallpaper
# 5: Span the wallpaper
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name WallpaperStyle -Value 0

# Apply changes
RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters ,1 ,True

Create a new key

mkdir xx

Remove a key

Use Remove-Item (alias rm).

rm xx
# remove a key and all its subkeys and values
rm -Recurse xx
# remove a key and all its subkeys and values
rm HKLM:\SOFTWARE\xx -Recurse
# remove a key's subkeys and values
rm HKLM:\SOFTWARE\xx\* -Recurse

Create a new value

cd hkcu:\Software\Microsoft\Windows\CurrentVersion\Explorer\Wallpapers\
New-ItemProperty -Path . -Name xx -Type DWORD -Value 1
PowerShell registry 20210607220324
New-ItemProperty

Clear a value

Clear-ItemProperty -path . -Name BackgroundHistoryPath0

Clear all values of a key

Clear-Item .

Remove a value

Use Remove-ItemProperty (alias rp).

rp -Path hkcu:\Software\Microsoft\Windows\CurrentVersion\Explorer\Wallpapers\ -Name BackgroundHistoryPath1

Rename key

Rename-Item

$path = "HKLM:\SOFTWARE\xxkey"
Rename-Item -Path $path -NewName xxkey2

Rename value

Rename-ItemProperty

$path = "HKLM:\SOFTWARE\xxkey"
Rename-ItemProperty -Path $path -Name xxVal2 -NewName xxVal3

Copy key

Copy-Item -Path $path -Destination $dest

Copy value

Copy-ItemProperty -Path $path -Destination $dest -Name $name

Move key

Move-Item -Path $path -Destination $dest

Move value

Move-ItemProperty -Path $path -Destination $dest -Name $name