Showing posts with label registry. Show all posts
Showing posts with label registry. Show all posts

Tuesday, October 30, 2007

Stand alone registry functions library


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){$false} else {$true}
}
































































































Wednesday, July 18, 2007

Instant Regedit access

This is a variation of an older post Open In Regedit (IE Context Menu) . This helps when you navigate the registry provider and you want to open regedit in the current key your working on.

Regedit maintains the last opened key under HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit\LastKey subkey, and it automatically updates this subkey each time you close regedit.

We can takes advantage of that by updating the value of the key from PowerShell and then run regedit. To run the function simply pass it the $pwd variable. To reuse it, included it in your profile.

##### jump to regedit.exe in a specified key path ######

function jump-regedit{
param( [string]$path = $(throw "Please provide Path.") )
# close regedit if open
stop-process -n regedit -ea silentlycontinue
$regKey="HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit";
# sp = Set-ItemProperty
# iex = Invoke-Expression sp $regKey -name "LastKey" -value "My Computer\$(convert-path $path)";
iex "regedit.exe";
}
set-alias jreg jump-regedit
 

function Jump-Regedit{
    Stop-Process -name regedit -errorAction silentlycontinue
    $regKey="HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit"
    Set-ItemProperty $regKey -name LastKey -value "My Computer\$(convert-path $pwd)"
    regedit.exe
}

set-alias jreg jump-regedit

 

#### USAGE ####

PS HKLM:\SOFTWARE\Microsoft> jreg
 

Shay

Tuesday, January 23, 2007

Open In Regedit (IE Context Menu)

About a year ago I contributed a tool I wrote, called "Open In Regedit", to Daniel's Petri IT knowledgebase. (You can read Daniel's review here).

Since I run my own blog now, I decided to re-publish it here and also check if it will run on Windows Vista.

Open In Regedit is a Context-Menu enhancement for Internet Explorer. While browsing the net for some articles , KBs and other system related issues, often you will have to make some registry modifications.

Usually, you will write down copy-paste the key , open regedit.exe and navigate your way inside. Well, not anymore.

Open In Regedit enables you to highlight registry paths written inside web pages, right-click on them and launching regedit.exe with the specified key location already highlighted (depends if the key already exists).

I tested it on Win XP SP2 (IE6) and Windows Vista (Business edition IE7). I have found that the difference between a registry path in XP and Vista is the Prefix "My computer\path_to_key_or_value" and "Computer\ path_to_key_or_value " respectively.

Play with it, and if you find a bug or even extended it in your way, then fill free to drop a comment. You can download it here.

$hay