使用 Powershell 匯出 .reg 檔案中的金鑰

使用 Powershell 匯出 .reg 檔案中的金鑰

我需要更改 Windows 7 註冊表中的幾個鍵值,以便在啟動 VLC 的命令中新增參數。

幸運的是,所有鍵都是以 開頭的鍵的子鍵VLC.

在此輸入影像描述

必須編輯Open和 的命令。PlayWithVLC我在想:

  • 將金鑰匯出到 .reg 檔案中,
  • 在外部編輯值以新增--no-playlist-enqueue至行中
  • 重新匯入註冊表中的.reg 檔案。

我在 PowerShell 方面的技能有限,我認為程式碼應該是這樣的:

Get-ChildItem "Registry::HKCR" -Recurse -Force 
| where { $_.Name -match 'vlc.'}`
| ForEach-Object {
    try {
        <create .reg entry>
    }
    catch { }
}

但我現在被困住了。您能給我一些關於如何進一步進行的建議嗎?

答案1

好吧,有限的 PS 技能,你想自動搞亂註冊表。

呃……你確定嗎? 8-}

話雖如此。

您在此處顯示的內容很好,但您沒有顯示要設定的值或設定註冊表項的命令。

您可以使用這些 cmdlet 來處理登錄。

Get-Command -CommandType Cmdlet -Name '*item*'


CommandType     Name                  ModuleName
-----------     ----                  ----------
Cmdlet          Clear-Item            Microsoft.PowerShell.Management
Cmdlet          Clear-ItemProperty    Microsoft.PowerShell.Management
Cmdlet          Copy-Item             Microsoft.PowerShell.Management
Cmdlet          Copy-ItemProperty     Microsoft.PowerShell.Management
Cmdlet          Get-ChildItem         Microsoft.PowerShell.Management
Cmdlet          Get-Item              Microsoft.PowerShell.Management
Cmdlet          Get-ItemProperty      Microsoft.PowerShell.Management
Cmdlet          Move-Item             Microsoft.PowerShell.Management
Cmdlet          Move-ItemProperty     Microsoft.PowerShell.Management
Cmdlet          New-Item              Microsoft.PowerShell.Management
Cmdlet          Remove-Item           Microsoft.PowerShell.Management
Cmdlet          Remove-ItemProperty   Microsoft.PowerShell.Management
Cmdlet          Set-Item              Microsoft.PowerShell.Management
Cmdlet          Set-ItemProperty      Microsoft.PowerShell.Management

使用前請務必查看說明文件及其範例。

https://docs.microsoft.com/en-us/powershell/scripting/getting-started/cookbooks/working-with-registry-entries?view=powershell-6

https://blogs.technet.microsoft.com/heyscriptingguy/2015/04/02/update-or-add-registry-key-value-with-powershell

PSRemoteRegistry 1.0.0.0

此模組包含在本機或遠端電腦上建立、修改或刪除登錄子項目和值的功能。

https://www.powershellgallery.com/packages/PSRemoteRegistry/1.0.0.0

https://stackoverflow.com/questions/28076128/powershell-export-multiple-keys-to-one-reg-file

我們知道,如果不小心的話,弄亂註冊表可能會造成很大的傷害。因此,請先對其進行備份,以便在發生災難時可以進行恢復,或至少可以恢復到系統恢復點、虛擬機器檢查點/快照。

因此,這裡對您發布的程式碼進行了輕微修改,但不要將此視為最終修改,因為您需要決定需要採取哪些操作以及如何採取操作。

Get-ChildItem "Registry::HKCR" -Recurse -Force `
| where { $_.Name -match 'vlc.'}`
| ForEach-Object {
    try {
            'Target key to modify / export / whatever'
            $_.Name
            # 'Registry code here' -WhatIf # remove the whatif if you are sure you are good with what you have
    }
    catch { 
               Write-Warning -Message 'Key not accessible' 
               $_.Name
          }
}

相關內容