config-central/tools/Export-MpcHcSettings.ps1

42 lines
2.1 KiB
PowerShell

[CmdletBinding()]
param(
[string]$OutputPath = (Join-Path $PSScriptRoot '..\configs\mpc-hc\settings.json'),
[switch]$IncludePrivateValues
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$rootPath = 'HKCU:\Software\MPC-HC\MPC-HC'
if (-not (Test-Path -LiteralPath $rootPath)) { throw "MPC-HC registry settings were not found at '$rootPath'. Start MPC-HC and save its settings first." }
$root = Get-Item -LiteralPath $rootPath
$excludedSubkeys = '(?i)(^|\\)(MediaHistory|Recent File List|Favorites)(\\|$)'
$excludedValues = '(?i)(path|directory|folder|filename|lastopen|lastused|recent|url)'
$values = [Collections.Generic.List[object]]::new()
foreach ($key in @($root) + @(Get-ChildItem -LiteralPath $rootPath -Recurse)) {
$relativePath = $key.Name.Substring($root.Name.Length).TrimStart('\')
if (-not $IncludePrivateValues -and $relativePath -match $excludedSubkeys) { continue }
foreach ($name in $key.GetValueNames()) {
if ([string]::IsNullOrWhiteSpace($name)) { continue }
if (-not $IncludePrivateValues -and $name -match $excludedValues) { continue }
$kind = [string]$key.GetValueKind($name)
if ($kind -notin @('String', 'ExpandString', 'DWord', 'QWord', 'MultiString', 'Binary')) { continue }
$raw = $key.GetValue($name, $null, [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)
$value = switch ($kind) {
'Binary' { @([byte[]]$raw) }
'MultiString' { @([string[]]$raw) }
default { $raw }
}
$values.Add([ordered]@{ path = $relativePath; name = $name; type = $kind; value = $value })
}
}
$document = [ordered]@{ schemaVersion = 1; values = @($values | Sort-Object path, name) }
$fullOutputPath = [IO.Path]::GetFullPath($OutputPath)
[void](New-Item -ItemType Directory -Path (Split-Path -Parent $fullOutputPath) -Force)
$document | ConvertTo-Json -Depth 16 | Set-Content -LiteralPath $fullOutputPath -Encoding utf8NoBOM
Write-Host "Exported $($values.Count) MPC-HC values to '$fullOutputPath'."
Write-Warning 'Review the file for machine-specific paths or private history before committing it.'