You are not logged in.

#1 09 Nov 2018 11:26

wquatan
Member
Registered: 19 Jun 2012
Posts: 16

Win10 Registry Script

I'm trying to find a script which allows me to modify Keys / Values in the registry.

What I have in mind (for easy reading the complete Hive till valuename is referred to as [NAME]:

1) allows modification of any [NAME] Valuetype (DWORD / Boolean / ...) including the Hex-value ones
2) copies [NAME] to [NAME]_Original (only when [NAME]_Original does not exist --> so only the first time and will contain the Windows-default)
3) copies [NAME] to [NAME]_Previous (obveous, will always contain the previous value)
4) modify [NAME] to the provided value

Does anyone knows scripts (or other tool) allowing to do that in batch ?

Because I haven't PowerShell experience, and because we're talking about the registry I would like to minimize risks involved by trial-and-error

Thanks

Offline

#2 12 Nov 2018 13:18

Pyprohly
Member
Registered: 26 Nov 2014
Posts: 37

Re: Win10 Registry Script

This script ought to help you out a little.

$scriptName = 'Get-RegistryKey'
$installedScript = Get-InstalledScript -Name $scriptName -ErrorAction SilentlyContinue
if (!$installedScript) {
	Install-Script -Name $scriptName -WarningAction Stop
	$installedScript = Get-InstalledScript -Name $scriptName -ErrorAction Stop
}
. (Join-Path -Path $installedScript.InstalledLocation -ChildPath ($scriptName + '.ps1'))

function SafeRegistryValueSet {
	[CmdletBinding()]
	param(
		[Parameter(Mandatory)]
		[Alias('PSPath', 'KeyName')]
		[string]
		$Path,

		[Parameter(Mandatory)]
		[string]
		[Alias('ValueName')]
		$Name,

		[Parameter(Mandatory)]
		[Object]
		$Data,

		[Microsoft.Win32.RegistryValueKind]
		$ValueKind = [Microsoft.Win32.RegistryValueKind]::Unknown
	)

	if ($regkey = Get-RegistryKey -Path $Path) {
		if ($existingData = $regkey.GetValue($Name)) {
			$regkey.SetValue($Name + '_Previous', $existingData, $ValueKind)

			if (!$regkey.GetValueNames().Contains($Name + '_Original')) {
				$regkey.SetValue($Name + '_Original', $existingData, $ValueKind)
			}
		}

		$regkey.SetValue($Name, $Data, $ValueKind)
	} else {
		Write-Error "The registry key '$Path' could not be opened."
	}
}

$entries = (
	('HKCU:\test', 'foo', 1),
	('HKCU:\test', 'bar', 2),
	('HKCU:\test', 'baz', 3)
) | % {
	@{
		Path = $_[0]
		Name = $_[1]
		Data = $_[2]
	}
}

foreach ($entry in $entries) {
	SafeRegistryValueSet @entry
}

Offline

Board footer

Powered by