Stand alone registry functions library

October 30, 2007

11 comments

EDIT [15/01/2010]  A newer version of this library is available for PowerShell 2.0.

 

The following file (also included below) contains 20 stand alone registry functions to Read,Write,Delete,Test registry keys/values from local or remote computer. Here’s a list of the functions:

Get-RegBinary
Get-RegDWord
Get-RegDefault
Get-RegExpandString
Get-RegMultiString
Get-RegQuadWord
Get-RegString
Get-RegValueKind
New-RegSubKey
Remove-RegSubKey
Remove-RegSubKeyTree
Set-RegBinary
Set-RegDWord
Set-RegDefault
Set-RegExpandString
Set-RegMultiString
Set-RegQuadWord
Set-RegString
Test-RegSubKey
Test-RegValue

 

For all functions, values for registry hive can be one of the enum values for
[Microsoft.Win32.RegistryHive]. To get a list of possible values type:
[enum]::getnames([Microsoft.Win32.RegistryHive])

Values for registry value kind can be one of the enum values for
[Microsoft.Win32.RegistryValueKind]. To get a list of possible kind values type:
[enum]::getnames([Microsoft.Win32.RegistryValueKind])

NOTE: get/set the CurrentUser hive on a remote server is N/A

Your comments/suggestions will be much appreciated. Please report any bugs to this post in the comments below.
Also, ping remote computers before running remote registry functions. You can read more about it here.

To load all functions in one step, "dot source" the file in the console or in your PowerShell profile. Type:

. <path_to_file.ps1>

 

Some code examples (using positioned parameters):

# create new sub key
New-RegSubKey . CurrentUser software\CustomRegKey

# set the default value
Set-RegDefault . CurrentUser software\CustomRegKey "This is the Default value";

# create String value
Set-RegString . CurrentUser software\CustomRegKey RegString "Shay Levi";

# create Multiple string value
Set-RegMultiString . CurrentUser software\CustomRegKey RegMultiString @("Power","Shell","Rocks!");

# create binary value
Set-RegBinary . CurrentUser software\CustomRegKey RegBinary @([char[]]"PowerShell");

# create dword value
Set-RegDWord . CurrentUser software\CustomRegKey RegDWord 123;

# create expand value
Set-RegExpandString . CurrentUser software\CustomRegKey RegExpandString "Username is %USERNAME%";

# create quadword value
Set-RegQuadWord . CurrentUser software\CustomRegKey RegQuadWord 10;

# test existence of a key named "notexist", locally, under hkcu:\softaware, returns $false
Test-RegSubKey . CurrentUser software\notexist

 

reg

 

Now, get the values:

# get the default value
PS > Get-RegDefault . CurrentUser software\CustomRegKey
This is the Default value

PS > # Get String value
PS > Get-RegString . CurrentUser software\CustomRegKey RegString
Shay Levi

# Get Multiple string value, returns array object
PS > Get-RegMultiString . CurrentUser software\CustomRegKey RegMultiString
Power
Shell
Rocks!

# Get binary value
PS > Get-RegBinary . CurrentUser software\CustomRegKey RegBinary
80
111
119
101
114
83
104
101
108
108

 

# Get Expand String value, not expanded
PS > Get-RegExpandString . CurrentUser software\CustomRegKey RegExpandString
Username is %USERNAME%

# Get Expand String value, expanded
PS > Get-RegExpandString . CurrentUser software\CustomRegKey RegExpandString -expand

Username is ShayL

# Get quadword value
PS > Get-RegQuadWord . CurrentUser software\CustomRegKey RegQuadWord;
10

#Get non existent value name, returns the default string defind by $defaultValue
PS > Get-RegString . CurrentUser software\CustomRegKey NonExistValueName "my default"
my default

 

####################################################

function Get-RegString{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [object]$defaultValue="Your default value"
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);

    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.GetValue($valueName,$defaultValue);
}

###############################################################################
# Function: Set-RegString
# Description: Create/Update the specified registry string value
# Return Value: True/false respectively

function Set-RegString{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [string]$value   
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName,$true);

    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.SetValue($valueName, $value, [Microsoft.Win32.RegistryValueKind]::String);
    if($?) {$true} else {$false}
}

###############################################################################
# Function: Get-RegMultiString
# Description: Gets an array strings (REG_MULTI_SZ)
# Return Value: Array object

function Get-RegMultiString{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [object]$defaultValue="Your default value"
    )

    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);

    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.GetValue($valueName,$defaultValue);
}

###############################################################################
# Function: Set-RegMultiString
# Description: Create/Update the specified registry as strings array  (REG_MULTI_SZ)
# Return Value: True/false respectively

function Set-RegMultiString{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [String[]]$value   
    )

    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName,$true);

    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.SetValue($valueName, $value,[Microsoft.Win32.RegistryValueKind]::MultiString);
    if($?) {$true} else {$false}
}

###############################################################################
# Function: Get-RegBinary
# Description: Gets the registry value (REG_BINARY)
# Return Value: Array object

function Get-RegBinary{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [object]$defaultValue="Your default value"
    )

    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);
    $subKey.GetValue($valueName,$defaultValue);
}

###############################################################################
# Function: Set-RegBinary
# Description: Create/Update the registry value (REG_BINARY)
# Return Value: True/false respectively

function Set-RegBinary{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [byte[]]$value   
    )

    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName,$true);

    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.SetValue($valueName, $value,[Microsoft.Win32.RegistryValueKind]::Binary);
    if($?) {$true} else {$false}

}

###############################################################################
# Function: Set-RegDWord
# Description: Create/Update the registry value (REG_DWORD)
# Return Value: True/false respectively

function Set-RegDWord{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [double]$value   
    )

    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName,$true);

    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.SetValue($valueName, $value,[Microsoft.Win32.RegistryValueKind]::DWord);
    if($?) {$true} else {$false}
}

###############################################################################
# Function: Get-RegDWord
# Description: Gets the registry value (REG_DWORD)
# Return Value: registry dword value

function Get-RegDWord{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [object]$defaultValue="Your default value"
    )

    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);

    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.GetValue($valueName,$defaultValue);
}

###############################################################################
# Function: Set-RegExpandString
# Description: Create/Update the registry value (REG_EXPAND_SZ)
# Return Value: True/false respectively

function Set-RegExpandString{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [string]$value       
    )

    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName,$true);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.SetValue($valueName, $value,[Microsoft.Win32.RegistryValueKind]::ExpandString);
    if($?) {$true} else {$false}
}

###############################################################################
# Function: Set-RegExpandString
# Description: Get the registry value (REG_EXPAND_SZ)
# Return Value: registry value expanded or not based on -expand switch

function Get-RegExpandString{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [object]$defaultValue="Your default value",
        [switch]$expand
    )

    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);

    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    if($expand){
        $subKey.GetValue($valueName,$defaultValue);
    } else {
        $subKey.GetValue($valueName,$defaultValue,[Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames);
    }
}

###############################################################################
# Function: Get-RegQuadWord
# Description: get the registry value (REG_QWORD)
# Return Value: registry value

function Get-RegQuadWord{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [object]$defaultValue="Your default value"
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.GetValue($valueName,$defaultValue);
}

###############################################################################
# Function: Set-RegExpandString
# Description: Get the registry value (REG_QWORD)
# Return Value: True/false respectively

function Set-RegQuadWord{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [long]$value   
    )

    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName,$true);

    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.SetValue($valueName, $value,[Microsoft.Win32.RegistryValueKind]::QWord);
    if($?) {$true} else {$false}
}

###############################################################################
# Function: Get-RegDefault
# Description: Get the registry default value
# Return Value: registry default value

function Get-RegDefault{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName
    )

    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);

    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.GetValue($null);
}

###############################################################################
# Function: Set-RegDefault
# Description: Set the registry default value
# Return Value: True/false respectively

function Set-RegDefault{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        $value   
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName,$true);

    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }

    #$regKey.SetValue($null, $value,[Microsoft.Win32.RegistryValueKind]::String);
    $subKey.SetValue($null, $value,[Microsoft.Win32.RegistryValueKind]::String);
    if($?) {$true} else {$false}
}

###############################################################################
# Function: New-RegSubKey
# Description: Create the registry key
# Return Value: True/false respectively

function New-RegSubKey{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName
    )

    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    [void]$regKey.CreateSubKey($keyName);
    if($?) {$true} else {$false}
}

################################################################################
# Function: Remove-RegSubKey
# Description: Delete the registry key
# Return Value: Throws error in case the key doesnt exist

function Remove-RegSubKey{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName
    )

    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $regKey.DeleteSubKey($keyName,$true);
}

###############################################################################
# Function: Remove-RegSubKeyTree
# Description: Delete the registry key tree
# Return Value: None

function Remove-RegSubKeyTree{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName
    )

    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $regKey.DeleteSubKeyTree($keyName);
}
###############################################################################
# Function: Get-RegValueKind
# Description: Get the registry value type (e.g, string,dword etc)
# Return Value: None

function Get-RegValueKind{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName
    )

    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $regVal=$subKey.GetValueKind($valueName);

    if(!$regVal){
        write-error "The specified registry value does not exist.";
        return;
    } else {
        $regVal;
    }
}

###############################################################################
# Function: Test-RegSubKey
# Description: Test the existence of the registry key
# Return Value: True/false respectively

function Test-RegSubKey{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName
    )

    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);

    if(!$subKey){$false}  else {$true}
}

###############################################################################
# Function: Test-RegValue
# Description: Test the existence of the registry value
# Return Value: True/false respectively

function Test-RegValue{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName
    )

    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])

    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $regVal=$subKey.GetValue($valueName);
    if($regVal –eq $null){$false} else {$true}
}

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

11 comments

  1. Mark GiovannozziOctober 21, 2008 ב 02:21

    Wonderful! Exactly what I was looking for. Thank You!

    Reply
  2. tomDecember 19, 2009 ב 19:55

    Just heard about this in the getscripting Podcast im currently ctrying to catch up.

    Will definetivly have a look on this.

    Thanks for sharing and for your effort in the PS-Community

    Regards from cold & snowy Germany

    Reply
  3. ScriptFanaticDecember 19, 2009 ב 22:19

    Thanks Tom! I plan to release a PowerShell module soon where you can pipe functions from one to another.

    Reply
  4. Daniele MuscettaJanuary 10, 2010 ב 22:10

    Be aware of 32bit-to-64bit registry access issue when using Microsoft.Win32.Registry* .Net classes – I had to workaround it with dear-old WMI here
    http://www.muscetta.com/2009/09/10/mistery-of-lost-registry-values/

    Reply
  5. NameJanuary 12, 2010 ב 17:11

    PS C:\> Get-ItemProperty “HKLM:\SOFTWARE\PSRemoteRegistry”

    PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\PSRemoteRegistry
    PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE
    PSChildName : PSRemoteRegistry
    PSDrive : HKLM
    PSProvider : Microsoft.PowerShell.Core\Registry
    dword : 0

    PS C:\> (Get-ItemProperty “HKLM:\SOFTWARE\PSRemoteRegistry”).dword
    0
    PS C:\> Test-RegSubKey “.” “LocalMachine” “SOFTWARE\PSRemoteRegistry”
    True
    PS C:\> Test-RegValue “.” “LocalMachine” “SOFTWARE\PSRemoteRegistry” “dword”
    False
    PS C:\>

    Reply
  6. ScriptFanaticJanuary 13, 2010 ב 10:24

    Definitely a bug, thanks for reporting! I have added an item to PSRemoteRegistry Issue tracker, it will be fixed in the next release. Check the issue report for a workaround:

    http://code.msdn.microsoft.com/PSRemoteRegistry/WorkItem/View.aspx?WorkItemId=2

    Reply
  7. NameJanuary 13, 2010 ב 21:18

    Test-RegValue

    $regVal=$subKey.GetValue($valueName);
    #if(!$regVal){$false} else {$true}
    if($regVal -eq $null){$false} else {$true}

    Reply
  8. ScriptFanaticJanuary 13, 2010 ב 22:19

    Thanks! I updated the code.
    I’d love to get your feedback on the PSRemoteRegistry module.

    Reply
  9. tonyMarch 25, 2010 ב 16:01

    Does this work with registry files as in loading a hive? I want to edit the system registry in a mounted vhd file.

    thanks

    Reply
  10. ScriptFanaticMarch 25, 2010 ב 16:28

    No. You can’t use the functions to edit ‘offline’ registry files.

    Reply