PowerShell for the registry
Every cmdlet you need to read, create, change, delete and secure registry keys and values, with patterns for scripts at scale.
PowerShell exposes the registry as drives through the Registry provider: HKLM: and HKCU: exist by default. Keys behave like folders (items) and values like properties of those items.
Cmdlets at a glance
| Task | Cmdlet |
|---|---|
| List subkeys | Get-ChildItem (-Recurse, -Depth) |
| Open a key, see its values | Get-Item |
| Read values | Get-ItemProperty, Get-ItemPropertyValue |
| Create a key | New-Item (-Force creates missing parents) |
| Create a value with a type | New-ItemProperty -PropertyType DWord|QWord|String|ExpandString|MultiString|Binary |
| Change a value | Set-ItemProperty |
| Rename, copy, move, clear a value | Rename-ItemProperty, Copy-ItemProperty, Move-ItemProperty, Clear-ItemProperty |
| Delete a value / a key | Remove-ItemProperty / Remove-Item -Recurse |
| Copy, move, rename a key | Copy-Item, Move-Item, Rename-Item |
| Test for a key | Test-Path |
| Permissions | Get-Acl, Set-Acl |
| Run on many machines | Invoke-Command, Enter-PSSession |
Read
# all values of a key
Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' |
Select-Object ProductName, DisplayVersion, CurrentBuild, UBR
# one value, as a plain value
Get-ItemPropertyValue 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -Name PortNumber
# a missing value without a red error
(Get-ItemProperty 'HKLM:\SOFTWARE\Contoso' -ErrorAction SilentlyContinue).Mode
Windows 11 still reports "Windows 10" in ProductName; use CurrentBuild (22000 and later is Windows 11) to tell them apart.
Create and change (idempotent pattern)
$k = 'HKLM:\SOFTWARE\Policies\Contoso\App'
if (-not (Test-Path $k)) { New-Item -Path $k -Force | Out-Null }
New-ItemProperty -Path $k -Name Mode -Value 2 -PropertyType DWord -Force | Out-Null
New-ItemProperty -Path $k -Name Server -Value 'app01' -PropertyType String -Force | Out-Null
New-ItemProperty -Path $k -Name Paths -Value @('\\fs01\a','\\fs02\a') -PropertyType MultiString -Force | Out-Null
New-ItemProperty -Force overwrites an existing value and sets its type, which makes scripts safe to run repeatedly. Set-ItemProperty on a value that doesn't exist creates a REG_SZ, which is often the wrong type.
Delete
Remove-ItemProperty -Path 'HKLM:\SOFTWARE\Contoso\App' -Name OldSetting -ErrorAction SilentlyContinue
Remove-Item -Path 'HKLM:\SOFTWARE\Contoso\Legacy' -Recurse -WhatIf # drop -WhatIf when sure
Other hives and views
# HKEY_USERS and HKEY_CLASSES_ROOT have no drive by default
New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null
Get-ChildItem HKU:\
# provider path without a drive
Get-ChildItem 'Registry::HKEY_USERS\.DEFAULT\Control Panel\International'
# 32-bit view from 64-bit PowerShell
$base = [Microsoft.Win32.RegistryKey]::OpenBaseKey('LocalMachine', 'Registry32')
$base.OpenSubKey('SOFTWARE\Contoso').GetValue('Mode')
At scale: many servers
$servers = 'vda01','vda02','vda03'
Invoke-Command -ComputerName $servers -ScriptBlock {
Get-ItemProperty 'HKLM:\SOFTWARE\Citrix\VirtualDesktopAgent' -Name ListOfDDCs -ErrorAction SilentlyContinue |
Select-Object @{n='Computer';e={$env:COMPUTERNAME}}, ListOfDDCs
} | Format-Table
PowerShell remoting (WinRM) is the preferred way to manage the registry remotely: it is encrypted, uses Kerberos, and doesn't need the Remote Registry service. See Remote registry.
Gotchas
- Paths with spaces need quotes. Single quotes stop PowerShell from expanding
$in value names. - Key names are paths: a value name can contain a backslash, a key name can't.
- In a script running as SYSTEM,
HKCU:is the SYSTEM profile. Write toHKU:\<SID>for a signed-in user.
Reference: about_Registry_Provider (Microsoft Learn).