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

PowerShell: Assign Guest Virtual Machine to a Cloud in VMM

# assignVmsToCloud.ps1 # version 0.02 # The following function assigns a guest VM into a…

Recovering Hard Drive Corrupted Files

Method 1: use TestDisk (very effective in recovering JPEG and partial files) Download https://www.cgsecurity.org/wiki/TestDisk_Download >…

PowerShell: Purge OneDrive

# purgeOneDrive_v0.0.1.ps1function purgeOneDrive{ write-host "Halting OneDrive and explorer processes..." taskkill.exe /F /IM "OneDrive.exe" taskkill.exe /F…