Skip to content

Instantly share code, notes, and snippets.

@damirarh
Created January 17, 2015 15:14
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save damirarh/ca2e9fb4df6df665c865 to your computer and use it in GitHub Desktop.
Save damirarh/ca2e9fb4df6df665c865 to your computer and use it in GitHub Desktop.
Function for changing a windows Store app setting directly in its settings.app file
function Set-WinStoreSetting {
param (
[Parameter(Mandatory=$true, Position=0)][string]$PackageName,
[Parameter(Mandatory=$true, Position=1)][string]$SettingName,
[Parameter(Mandatory=$true, Position=2)][string]$SettingValue
)
$settingsFile = [IO.Path]::Combine($env:LOCALAPPDATA, 'Packages', $PackageName, 'Settings\settings.dat')
# temporary paths
$regFile = ".\settings.reg"
$registryImportLocation = "HKLM\_TMP"
reg load $registryImportLocation $settingsFile
reg export $registryImportLocation $regFile
$fileContents = Get-Content $regFile
$settingNamePrefix = """$SettingName""="
$finalContents = @()
$processing = $false
Foreach ($line in $fileContents)
{
If (-Not ($processing))
{
# scanning for first line of the value
If ($line.StartsWith($settingNamePrefix))
{
# found - switch mode and start reading the old value
$processing = $true
$oldValue = $line.Replace($settingNamePrefix, "")
}
Else
{
# not found yet - copy to output
$finalContents += $line
}
}
Else
{
# non-first lines have leading spaces
$oldValue += $line.TrimStart(" ")
}
If ($processing)
{
# scanning for last line of the value
If ($oldValue.EndsWith("\"))
{
# strip trailing backslash; the value continues
$oldValue = $oldValue.TrimEnd("\")
}
Else
{
# no backslash; the value is complete
# extract type and timestamp from old value
$match = $oldValue -match '(.*:)(.*)'
$valueType = $matches[1]
$timestamp = $matches[2].Substring($matches[2].Length - 23)
# serialize the new value
$utfEncoded = [System.Text.Encoding]::Unicode.GetBytes($SettingValue)
Foreach ($byte in $utfEncoded)
{
$serializedValue += [System.Convert]::ToString($byte, 16).PadLeft(2, "0") + ","
}
# append null terminator
$serializedValue += "00,00,"
$newValue = $valueType + $serializedValue + $timestamp
$finalContents += "$settingNamePrefix$newValue"
$processing = $false
}
}
}
$finalContents | Out-File $regFile
reg import $regFile
reg unload $registryImportLocation
Remove-Item $regFile
}
@fragtion
Copy link

fragtion commented May 8, 2021

This is great for editing a value. How can we read the unencoded values ? It would be nice if the script could output a human-readable .reg file which can then be re-encoded and imported, instead of purely editing a single value in the dark

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment