4

I am trying to check if a key-structure exists in the registry using powershell. If the structure does not exist, I need to create it and then I need to create the keys in the ending folder. If I run the snippets individually to create the keys, they create just fine. But running the block itself (ensuring manually within the registry that the keys don't exist) it won't create the folder structure. Not sure what the issue is. Any help would be appreciate. The code is as follows:

$Registry_Paths = "hkcu:\Software\Microsoft\Office\14.0", "hkcu:\Software\Microsoft\Office\14.0\Groove", "hkcu:\Software\Microsoft\Office\14.0\Groove\Development"

foreach($Registry_Path in $Registry_Paths)
{
$Test_Path_Result = Test-Path -Path $Registry_Path
if($Test_Path_Result -eq $false)
{
    $Registry_Key_Log += "Warning: No registry key path found at " + $Registry_Path +"`n"
    $Registry_Key_Log += "Creating key now for " + $Registry_Path + "`n" + "`n"

    if($Registry_Path -eq "hkcu:\Software\Microsoft\Office\14.0")
    {
        try{
        New-Item -Path "HKCU:\Software\Microsoft\Office\14.0" -ItemType Key
        }
        catch
        {
            $Error_Log += "Warning: There was an error when attempting to create a new registry key, or key property for $Registry_Path"
            $Error_Log += $_.exception.message
        }

    }
    if($Registry_Path -eq "hcku:\Software\Microsoft\Office\14.0\Groove")
    {
        try{
        New-Item -Path "HKCU:\Software\Microsoft\Office\14.0\Groove" -ItemType Key
        }
        catch
        {
            $Error_Log += "Warning: There was an error when attempting to create a new registry key, or key property for $Registry_Path"
            $Error_Log += $_.exception.message
        }
    }
    if($Registry_Path -eq "hcku:\Software\Microsoft\Office\14.0\Groove\Development")
    {
        try{
        New-Item -Path "HKCU:\Software\Microsoft\Office\14.0\Groove\Development" -ItemType Key
        New-ItemProperty -Path "hkcu:\Software\Microsoft\Office\14.0\Groove\Development" -Value 00000001 -PropertyType dword -Name "EnableReleaseBuildDebugOutput"
        New-ItemProperty -Path "hkcu:\Software\Microsoft\Office\14.0\Groove\Development" -Value 1 -Name "TraceIdentityMessaging"
        New-ItemProperty -Path "hkcu:\Software\Microsoft\Office\14.0\Groove\Development" -Value 00000001 -PropertyType dword -Name "TraceTelespaceFetch"
        New-ItemProperty -Path "hkcu:\Software\Microsoft\Office\14.0\Groove\Development" -Value 1 -Name "TraceConnectSequence"

        }
        catch
        {
            $Error_Log += "Warning: There was an error when attempting to create a new registry key, or key property for $Registry_Path"
            $Error_Log += $_.exception.message
        }
    }

}
}
1
  • Could you be clearer about which "snippets" worked and which ones didn't? Is it failing to create or failing to detect? Commented Nov 3, 2014 at 3:22

3 Answers 3

7

Here is how I'd do it.

$Key = "HKEY_CURRENT_USER\TEST"
If  ( -Not ( Test-Path "Registry::$Key")){New-Item -Path "Registry::$Key" -ItemType RegistryKey -Force}
Set-ItemProperty -path "Registry::$Key" -Name "Less" -Type "String" -Value "Less"
Sign up to request clarification or add additional context in comments.

Comments

2
    function New-RegKey 
    {
        # Create subkey in specified registry hive/key.
        param (
        [string]$Key
        )
        # Check if key exists or not:
        if ($reg.GetSubKeyNames() -notcontains $Key) 
        {
        Try 
            {
            # The key DOES NOT exist, so create it:
            $Script:reg = $reg.CreateSubKey($Key)
            Write-Host "Registry key ""$($Script:reg.Name)"" has been successfully created." -foregroundColor Green
            }
        Catch 
            {
            Write-Host "ERROR:$((($ERROR[0].Exception).InnerException).Message)."   -ForegroundColor Yellow
            }
        }
        else 
        {
        Write-Host "Registry key ""$Key"" already exists. Do not need to create." -ForegroundColor White
        # Open the subkey in writable mode and update the $reg in script scope:
        Try 
            {
            $Script:reg = $reg.OpenSubKey($Key, $Writable)
            }
        Catch 
            {
            Write-Host "ERROR:$((($ERROR[0].Exception).InnerException).Message)." 
            -ForegroundColor Yellow
            }
        }
    }

    function Set-RegValue 
    {
    param (
    [string]$RegName,
    [string]$RegValue,
    [string]$RegValueType
    )
    # Create or update the specified registry value (valueName and valueValue):
    # First check to see if the value name exists or not in the current key:
        if ($reg.GetValueNames() -notcontains $RegName) {
        # Registry value does not exist, so create one and assign value:
            Try 
            {
            $reg.SetValue($RegName, $RegValue, $RegValueType)
            Write-Host "Registry value name ""$RegName"" does not exist, so create it and assign value ""$RegValue"" with type: ""$RegValueType""."
            }
            Catch 
            {
            Write-Host "ERROR:  $((($ERROR[0].Exception).InnerException).Message)."         -ForegroundColor Yellow
            }
        }
        else 
        {
            Try 
            {
            Write-Host "Registry value name ""$RegName"" already exists, so update its value ""$RegValue"" with type: ""$RegValueType""."
            $reg.SetValue($RegName, $RegValue, $RegValueType)
            }
            Catch 
            {
            Write-Host "ERROR:$((($ERROR[0].Exception).InnerException).Message)." -ForegroundColor Yellow
            }
        }
    }

Comments

1

I got it working. Turns out I had a typo (hkcu != hcku):

if($Registry_Path -eq "hkcu:\Software\Microsoft\Office\14.0")

if($Registry_Path -eq "hcku:\Software\Microsoft\Office\14.0\Groove")

Note the "HKCU" and "HCKU" above.

1 Comment

you are still doing it the hard way. For instance, you could just create the hkcu:\Software\Microsoft\Office\14.0\Groove\Development key with the -Force command and it will create all the parent keys at the same time, saving you about 8-10 lines of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.