PowerShell: Create Registry Keys within Windows Localhost

# createRegKey.ps1

$regKeys=@(
	@{
        hive='REGISTRY::HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome';
        name='ChromeCleanupEnabled';
        value=0
    }
	@{
        hive='REGISTRY::HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome';
        name='ChromeCleanupReportingEnabled';
        value=0
        }
)
 
function createRegKey{
    param(
        $regHive,
        $keyName,
        $value
    )
    try{
        $keyValue=(Get-ItemProperty -Path $regHive -Name $keyName -ErrorAction Stop).$keyName
        if($keyValue -ne $value){
            New-ItemProperty -Path $regHive -Name $keyName -Value $value -Type String -Force
        }else{
            write-host "$regHive $keyName already has a value of $value"
        }
    }catch [System.Management.Automation.ItemNotFoundException],[System.Management.Automation.PSArgumentException] {
        New-Item -Path $regHive -Force
        New-ItemProperty -Path $regHive -Name $keyName -Value $value -Force
        write-host "$regHive $keyName value has been set to $value"
    }catch {
        write-warning $_
        New-ItemProperty -Path $regHive -Name $keyName -Value $value -Type String -Force
        write-host "$regHive $keyName value has been set to $value"
    }
}

$regKeys|%{createRegKey $_.hive $_.name $_.value}

Leave a Reply

Your email address will not be published. Required fields are marked *