Examples
The following simple examples attempt to demonstrate how the
registry can be read and written to with
PowerShell.
Create a new key
$key_tq84 = new-item -path HKCU:\Software -name tq84
$key_tq84.GetType().FullName
Set key's default value
$null = set-item -path HKCU:\Software\tq84 -value "This is the default value"
Add a named value
$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
$val_num = get-itemPropertyValue HKCU:\Software\tq84 -name num
write-host "The value of num is $val_num"
$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