Posted On January 10, 2023

PowerShell: Create Registry Keys within Windows Localhost

kimconnect 0 comments
blog.KimConnect.com >> Codes , Windows >> 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 *

Related Post

How to Install & Configure Pihole on Ubuntu 20.04

1. Installation- Run these commands: # sudo apt-get install gamin -ysudo curl -sSL https://raw.githubusercontent.com/pi-hole/pi-hole/master/automated%20install/basic-install.sh |…

Add Chocolatey Private Repo

$privateRepo='kimconnectrepo' $privateRepoUrl='https://choco.kimconnect.net/chocolatey' $priority=1 # Set private repo source if it doesn't exist function addRepo{ param(…

PowerShell: Install Windows Cluster Admin

# Install Microsoft Clustering ManagementFunction installClusteringManagment{ # Set PowerShell Gallery as Trusted to bypass prompts…