如何实现右键注册表分支生成PowerShell注册表导入脚本?
实现注册表右键生成PowerShell导入脚本的方案
方法1:添加右键菜单选项(永久生效)
运行以下PowerShell脚本,直接给注册表编辑器添加「生成PowerShell导入脚本」的右键选项:
# 创建右键菜单项注册表路径 $menuPath = "HKCR:\Registry\shell\Export-PSScript" New-Item -Path $menuPath -Force | Out-Null Set-ItemProperty -Path $menuPath -Name "(Default)" -Value "生成PowerShell导入脚本" Set-ItemProperty -Path $menuPath -Name "Icon" -Value "powershell.exe,0" # 设置右键菜单触发命令 $commandPath = "$menuPath\command" New-Item -Path $commandPath -Force | Out-Null Set-ItemProperty -Path $commandPath -Name "(Default)" -Value @" powershell.exe -NoExit -Command "`$keyPath = '%1'; `$outputPath = Join-Path (Get-Location) (`$keyPath.Split('\')[-1] + '_RegImport.ps1'); & { param(`$KeyPath, `$OutputPath) function Export-RegToPS { param(`$Key, `$Indent) `$indentStr = ' ' * `$Indent `$output = @() `$output += "`$indentStr`if (-not (Test-Path `$Key)) { New-Item -Path `$Key -Force | Out-Null }" # 导出当前键的所有值项 foreach (`$value in Get-ItemProperty -Path `$Key -ErrorAction SilentlyContinue) { foreach (`$prop in `$value.PSObject.Properties | Where-Object { `$_.Name -notin @('PSPath','PSParentPath','PSChildName','PSDrive','PSProvider') }) { `$valName = `$prop.Name `$valData = `$prop.Value `$valType = (Get-ItemPropertyValue -Path `$Key -Name `$valName -ErrorAction SilentlyContinue | Get-Member -MemberType Property).TypeName # 适配不同值类型的PowerShell语法 switch (`$valType) { 'String' { `$valData = "`"`$valData`"" } 'Int32' {} 'UInt32' {} 'ByteArray' { `$valData = '@(' + (`$valData -join ', ') + ')' `$valType = 'Binary' } default { `$valType = 'String' } } `$output += "`$indentStrSet-ItemProperty -Path `$Key -Name '$valName' -Value `$valData -Type `$valType -Force" } } # 递归导出子键 foreach (`$subKey in Get-ChildItem -Path `$Key -ErrorAction SilentlyContinue) { `$output += Export-RegToPS -Key `$subKey.PSPath -Indent (`$Indent + 1) } return `$output } `$scriptContent = Export-RegToPS -Key `$KeyPath -Indent 0 `$scriptContent | Out-File -FilePath `$OutputPath -Encoding utf8 Write-Host "脚本已生成:" `$OutputPath }" "@
执行完成后,打开注册表编辑器右键任意分支,就能看到新增的选项。点击后会在当前PowerShell的工作目录生成以分支名称命名的PS导入脚本,脚本包含创建子键、设置所有键值的完整逻辑。
方法2:临时转换.reg文件为PS脚本
如果不想添加右键菜单,可先将目标分支导出为.reg文件,再用以下脚本转换成PowerShell导入脚本:
# 替换为你的.reg文件路径和输出PS脚本路径 $regFilePath = "C:\temp\exported.reg" $psFilePath = "C:\temp\reg_import.ps1" $regContent = Get-Content $regFilePath -Raw $psContent = @() $currentKey = $null # 逐行解析.reg文件内容 $regContent -split "`r`n" | ForEach-Object { $line = $_.Trim() if ($line -match '^\[(.*)\]$') { $currentKey = $matches[1] $psContent += "if (-not (Test-Path '$currentKey')) { New-Item -Path '$currentKey' -Force | Out-Null }" } elseif ($line -match '^"([^"]+)"=(.*)$' -and $currentKey) { $valName = $matches[1] $valData = $matches[2] $valType = 'String' # 处理不同值类型的转换 if ($valData -match '^dword:(.*)$') { $valData = [uint32]$matches[1] $valType = 'DWord' } elseif ($valData -match '^hex:(.*)$') { $valData = '@(' + ($matches[1] -split ',' | ForEach-Object { "0x$_" }) -join ', ' + ')' $valType = 'Binary' } elseif ($valData -match '^hex\(7\):(.*)$') { $bytes = [byte[]]($matches[1] -split ',' | ForEach-Object { [byte]"0x$_" }) $valData = "`"$( [System.Text.Encoding]::Unicode.GetString($bytes) )`"" } $psContent += "Set-ItemProperty -Path '$currentKey' -Name '$valName' -Value $valData -Type $valType -Force" } } $psContent | Out-File -FilePath $psFilePath -Encoding utf8
替换路径后运行脚本,即可得到可直接执行的PowerShell导入脚本。
内容的提问来源于stack exchange,提问作者Nick
相关产品推荐
相关产品推荐

