Search notes:

PowerShell: Read and write from/to the registry

Two default drives

By default, PowerShell defines two drives to access the registry:
In order to add a new drive, the new-psDrive cmdlet can be used:
PS C:\> new-psDrive -name HKCR -psProvider registry -root HKEY_CLASSES_ROOT

Examples

The following simple examples attempt to demonstrate how the registry can be read and written to with PowerShell.

Create a new key

A new registry key can be created with the new-item.
If the item being created is a registry key, the cmdlet returns a Microsoft.Win32.RegistryKey object. This object is assigned to the variable $key_tq84:
$key_tq84 = new-item -path HKCU:\Software -name tq84

$key_tq84.GetType().FullName

Set key's default value

A key's default value can be set with set-item:
$null = set-item -path HKCU:\Software\tq84 -value "This is the default value"

Add a named value

A value can be added to a key with the cmdlet set-itemProperty.
$null = new-itemProperty -path HKCU:\Software\tq84 -value "Hello world" -name txt

Add another value

Another value is added. Although the value is «numeric», without explicitly stating its data type, the value is added as a string (REG_SZ):
$null = new-itemProperty -path HKCU:\Software\tq84 -name someValue -value 99

Add a numeric value

This type, the data type of the added value is explicitly specified with the -propertyType flag as REG_DWORD:
$null = new-itemProperty -path HKCU:\Software\tq84 -name num -value 42 -propertyType DWord

Using .NET Objects to create values

Registry values and keys can not only be created with cmdLets. It's also possible with the methods SetValue() and CreateSubKey() of the .NET class Microsoft.Win32.RegistryKey (of which an instance is already assigned to the variable $key_tq84):
$key_tq84.SetValue('V',    1 )
$key_tq84.SetValue('W', 'xyz')

$sub_key = $key_tq84.CreateSubKey('subKey')
$sub_key.SetValue('', "sub key's default value")

Values created

The registry values that were created look like this in regedit.exe

Read a value

A value in the registry can be read with get-itemPropertyValue:
$val_num = get-itemPropertyValue HKCU:\Software\tq84 -name num
write-host "The value of num is $val_num"
It is also possible to read one or more values at once with the get-itemProperty cmdlet which returns a System.Management.Automation.PSCustomObject object:
$values = get-itemProperty HKCU:\Software\tq84 -name num,txt
$values.GetType().FullName
#
# System.Management.Automation.PSCustomObject

write-host "The value of num is $($values.num)"
write-host "The value of txt is $($values.txt)"

Reading the default value

A key's default value can be read by reading its property named (default):
get-itemPropertyValue HKCU:\Software\tq84 -name '(default)'

Check if a value or key exists

The existence of a registry key can be checked with test-path. The existence of a value can be checked by calling $regKey.GetValue(): if it returns $null, the value does not exist.
The first key tested is the one we just created, the second key (HKCU:\a\path\that\likely\does\not\exist) does (most probably) not exits.
if (test-path HKCU:\Software\tq84) {

    write-output "The registry key HKCU\Software\tq84 exists"

    $key = get-item HKCU:\Software\tq84

    if ($key.getValue('num ') -ne $null) {
        write-output "value num in key HKCU\Software\tq84"
    }
    else {
        write-output 'num does not exist in key HKCU\Software\tq84'
    }

    if ($key.getValue("unobtainium") -ne $null) {
        write-output "unobtainium exists in key HKCU\Software\tq84"
    }
    else {
        write-output "unobtainium does not exist in key HKCU\Software\tq84"
    }

}
else {
    write-output "The registry key HKCU\Software\tq84 does not exist"
}


if (test-path HKCU:\a\path\that\likely\does\not\exist) {
    write-output "The registry key 'HKCU\a\path\that\likely\does\not\exist' exists"
}
else {
    write-output "The registry key 'HKCU\a\path\that\likely\does\not\exist' does not exist"
}

Cleaning up

Time to clean up the mess…
#
#  Delete a value (value three) under a given key (HKCU\Software\tq84)
#
#q remove-itemProperty -path HKCU:\Software\tq84 -name "value three"

#
#  Delete an entire key (with all its remaining values)
#
remove-item         -path HKCU:\Software\tq84  -recurse

Binary data

Binary data might be written by specifying an array of bytes and using the -type binary option:
new-itemProperty -path HKCU:\Software\tq84 -value  0xab, 0xcd, 0xef -name abcdef -type binary
Alternatively, a byte array can be passed as value:
[byte[]] $byte_array = 65,66,67
new-itemProperty -path HKCU:\Software\tq84 -value  $byte_array -name byte_array

Deleting an existing default value

A key's default value can be deleted like so (if it exists):
(get-item 'hkcu:\Software\tq84').OpenSubKey('', $true).DeleteValue('')

See also

Other PowerShell examples

Index