Home
POV-Ray
PowerShell pipeline
PowerShell scripts
Out-Excel
Start-Monitor
Get-LogEvents
GUI_ServerInfo
RegistryFunctions
code
Get-ChildItemToDepth
Get-SharesAndPerm...
PoSh punctuation
Contact
Sitemap
Links

 

________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

 <#
.SYNOPSIS
Changes the value of a registry-key.

.DESCRIPTION
Changes the value of a registry-key in a registry-path for a computer.
Creates path and key, if path or key could not be found.
Returns boolean to indicate success or failure.

.PARAMETER ComputerName
Specifies the computer name.
Defaults to local machine.

.PARAMETER Hive
Specifies the registry hive.
Valid entries are: HKCR, HKCU, HKLM, HKU, HKCC
Defaults to HKLM.

.PARAMETER Path
Specifies the registry path.

.PARAMETER Key
Specifies the name of the registry key.

.PARAMETER Value
Specifies the value for the registry key.

.PARAMETER Keytype
Specifies the type for the registry key.
Valid entries are: REG_SZ, REG_EXPAND_SZ, REG_BINARY, REG_DWORD, REG_MULTI_SZ
Defaults to REG_SZ.

.INPUTS
None. You cannot pipe objects to this function.

.OUTPUTS
This function returns True, if the key has the correct value.
Otherwise, it returns False.

.NOTES
This function uses WMI to access the registry.
The remote computer does not need to have Windows PowerShell installed for this function to work.

This function only handles the following registrykey types:
    REG_SZ, REG_EXPAND_SZ, REG_BINARY, REG_DWORD or REG_MULTI_SZ

Written by FurBall

.EXAMPLE
C:\ps> Set-RegistryValue "." "HKLM" "SOFTWARE\Quasar" "Background" "Blue" "REG_SZ"

Description
----------
Creates registrykey "HKLM\SOFTWARE\Quasar\Background" on local computer if it does not exist.
Changes type to REG_SZ.
Sets value to Blue.

.EXAMPLE
C:\ps> $myValue = @(1, 255, 0, 254)
C:\ps> Set-RegistryValue -Path "SOFTWARE\Quasar" -Key "Code" -Value $myValue -Keytype "REG_BINARY"

Description
----------
Creates registrykey "HKLM\SOFTWARE\Quasar\Code" on local computer if it does not exist.
Changes type to REG_BINARY.
Sets value to 01 ff 00 fe.

Parameter Hive         can be omitted, defaults to HKLM.
Parameter Computername can be omitted, defaults to local machine.

.EXAMPLE
C:\ps> Set-RegistryValue -Computername $strComputer -Hive "HKLM" -Path "SOFTWARE\Quasar" -Key "Build" -Value 1234567890 -Keytype "REG_DWORD"

Description
----------
Creates registrykey "HKLM\SOFTWARE\Quasar\Build" on remote computer if it does not exist.
Changes type to REG_DWORD.
Sets value to 0x499602d2 (1234567890).

.EXAMPLE
C:\ps> $arrActions = @("StopA", "StartB", "StartC")
C:\ps> Set-RegistryValue -Computername $strComputer -Hive "HKLM" -Path "SOFTWARE\Quasar" -Key "Actions" -Value $arrActions -Keytype "REG_MULTI_SZ"

Description
----------
Creates registrykey "HKLM\SOFTWARE\Quasar\Build" on remote computer if it does not exist.
Changes type to REG_MULTI_SZ.
Sets value to
StopA
StartB
StartC

.EXAMPLE
C:\ps> Set-RegistryValue "." "HKLM" "SOFTWARE\Quasar"  "ConfigUiPath" '%SystemRoot%\System32' "REG_EXPAND_SZ"

Description
----------
Creates registrykey "HKLM\SOFTWARE\Quasar\ConfigUiPath" on local computer if it does not exist.
Changes type to REG_EXPAND_SZ.
Sets value to %SystemRoot%\System32

.LINK
http://www.fourcats.nl/RegistryFunctions.html

#>
Function Set-RegistryValue
{Param (
    [Parameter(ValueFromPipeline=$false, Mandatory=$false, HelpMessage="Enter a computer name")][string]$ComputerName = $env:computername,
    [Parameter(ValueFromPipeline=$false, Mandatory=$false, HelpMessage="Enter hive: HKCR, HKCU, HKLM, HKU or HKCC")][string][ValidateSet("HKCR", "HKCU", "HKLM", "HKU", "HKCC")]$Hive = "HKLM",
    [Parameter(ValueFromPipeline=$false, Mandatory=$true, HelpMessage="Enter registry path")][string]$Path,
    [Parameter(ValueFromPipeline=$false, Mandatory=$true, HelpMessage="Enter registry key")][string]$Key,
    [Parameter(ValueFromPipeline=$false, Mandatory=$true, HelpMessage="Enter value for registry key")]$Value,
    [Parameter(ValueFromPipeline=$false, Mandatory=$false, HelpMessage="Enter key type: REG_SZ, REG_EXPAND_SZ, REG_BINARY, REG_DWORD or REG_MULTI_SZ")][string][ValidateSet("REG_SZ", "REG_EXPAND_SZ", "REG_BINARY", "REG_DWORD", "REG_MULTI_SZ")]$Keytype = "REG_SZ"
    )
     Switch ($Hive){
         {$_ -eq "HKCR"}    {$iHive = 2147483648 ; break}
         {$_ -eq "HKCU"}    {$iHive = 2147483649 ; break}
         {$_ -eq "HKLM"}    {$iHive = 2147483650 ; break}
         {$_ -eq "HKU"}        {$iHive = 2147483651 ; break}
         {$_ -eq "HKCC"}    {$iHive = 2147483653 ; break}
     }
    $wmi = [wmiclass]"\\$ComputerName\root\default:StdRegProv"
    $wmi.CreateKey($iHive, $Path)

     Switch ($Keytype){
        {$_ -eq "REG_SZ"}                { $wmi.SetStringValue($iHive, $Path, $Key, $Value) ; break }
        {$_ -eq "REG_EXPAND_SZ"}    { $wmi.SetExpandedStringValue($iHive, $Path, $Key, $Value) ; break }
        {$_ -eq "REG_BINARY"}        { $wmi.SetBinaryValue($iHive, $Path, $Key, $Value) ; break }
        {$_ -eq "REG_DWORD"}            { $wmi.SetDwordValue($iHive, $Path, $Key, $Value) ; break }
        {$_ -eq "REG_MULTI_SZ"}        { $wmi.SetMultiStringValue($iHive, $Path, $Key, $Value) ; break }
     }

    $result = Get-RegistryValue $ComputerName $Hive $Path $Key
    if ($Keytype -eq "REG_EXPAND_SZ"){
        if ($result -eq "not found") {return $false }
         else{    return $true}
    }
    else{
        if ((Compare-Object $result $Value) -eq $null) {return $true}
         else{    return $false}
     }
}

<#
.SYNOPSIS
Returns the value of a registry-key.

.DESCRIPTION
Returns the value of a registry-key in a registry-path for a computer.
Returns "Not found" if computer, path or key could not be found.

.PARAMETER ComputerName
Specifies the computer name.

.PARAMETER Hive
Specifies the registry hive.
Valid entries are: HKCR, HKCU, HKLM, HKU, HKCC
Defaults to HKLM.

.PARAMETER Path
Specifies the registry path.

.PARAMETER Key
Specifies the name of the registry key.

.INPUTS
None. You cannot pipe objects to this function.

.OUTPUTS
This function returns the value of the requested registry-key.
This function returns a string "Not found" if computer, path or key could not be found.

.NOTES
This function uses WMI to access the registry.
The remote computer does not need to have Windows PowerShell installed for this function to work.

This function only handles the following registrykey types:
    REG_SZ, REG_EXPAND_SZ, REG_BINARY, REG_DWORD or REG_MULTI_SZ

Written by FurBall

.EXAMPLE
C:\ps> Get-RegistryValue -Computername "." -Hive "HKU"  "S-1-5-20\Control Panel\Colors" -Key "Background"

Description
----------
Returns the value of HKEY_USERS\S-1-5-20\Control Panel\Colors\Background for local computer.

.EXAMPLE
C:\ps> Get-RegistryValue $strComputer "HKLM" "SOFTWARE\Acronis\afcdp" "build"

Description
----------
Returns the value of HKEY_LOCAL_MACHINE\SOFTWARE\Acronis\afcdp\build for (remote) computer.


.EXAMPLE
C:\ps> Get-RegistryValue -Path "SOFTWARE\Acronis\afcdp" -Key "build"

Description
----------
Returns the value of HKEY_LOCAL_MACHINE\SOFTWARE\Acronis\afcdp\build for local computer.

Parameter Hive         can be omitted, defaults to HKLM.
Parameter Computername can be omitted, defaults to local machine.


.LINK
http://www.fourcats.nl/RegistryFunctions.html

#>
Function Get-RegistryValue
{Param (
    [Parameter(ValueFromPipeline=$false, Mandatory=$false, HelpMessage="Enter a computer name")][string]$ComputerName = $env:computername,
    [Parameter(ValueFromPipeline=$false, Mandatory=$false, HelpMessage="Enter hive: HKCR, HKCU, HKLM, HKU or HKCC")][string][ValidateSet("HKCR", "HKCU", "HKLM", "HKU", "HKCC")]$Hive = "HKLM",
    [Parameter(ValueFromPipeline=$false, Mandatory=$true, HelpMessage="Enter registry path")][string]$Path,
    [Parameter(ValueFromPipeline=$false, Mandatory=$true, HelpMessage="Enter registry key")][string]$Key
    )
    $Reg_Sz = 1
    $Reg_Expand_Sz = 2
    $Reg_Binary = 3
    $Reg_Dword = 4
    $reg_Multi_Sz = 7
    $RequestedValue = "not found"
     Switch ($Hive){
         {$_ -eq "HKCR"}    {$iHive = 2147483648 ; break}
         {$_ -eq "HKCU"}    {$iHive = 2147483649 ; break}
         {$_ -eq "HKLM"}    {$iHive = 2147483650 ; break}
         {$_ -eq "HKU"}        {$iHive = 2147483651 ; break}
         {$_ -eq "HKCC"}    {$iHive = 2147483653 ; break}
     }
    $wmi = [wmiclass]"\\$ComputerName\root\default:StdRegProv"
    $RegValues = $wmi.EnumValues($iHive, $Path)
    for ($iIndex = 0 ; $iIndex -lt $RegValues.sNames.Count ; $iIndex++) {
        if ($RegValues.sNames[$iIndex] -eq $Key) {
            Switch ($RegValues.Types[$iIndex]) {
                {$_ -eq $Reg_Sz}        { $RequestedValue = ($wmi.GetStringValue($iHive, $Path, $Key)).sValue ; break }
                {$_ -eq $Reg_Expand_Sz} { $RequestedValue = ($wmi.GetExpandedStringValue($iHive, $Path, $Key)).sValue ; break }
                {$_ -eq $Reg_Binary}    { $RequestedValue = ($wmi.GetBinaryValue($iHive, $Path, $Key)).uValue ; break }
                {$_ -eq $Reg_Dword}     { $RequestedValue = ($wmi.GetDwordValue($iHive, $Path, $Key)).uValue ; break }
                {$_ -eq $reg_Multi_Sz}  { $RequestedValue = ($wmi.GetMultiStringValue($iHive, $Path, $Key)).sValue ; break }
           }
        }
    }
    return $RequestedValue
}

Export-ModuleMember -function Get-RegistryValue, Set-RegistryValue

to Top of Page

FurBall Productions | furball@casema.nl