由於登錄機碼是 Windows PowerShell 磁碟機上的項目,其處理方式與處理檔案和資料夾極為相似。最大的差別在於登錄型 Windows PowerShell 磁碟機上的每個項目都是容器,就像檔案系統磁碟機上的資料夾。然而,登錄項目及其關聯值則是項目的屬性,而非另一類型的項目。

列出登錄機碼的所有子機碼

使用 Get-ChildItem 可顯示登錄機碼當層內所有的項目。加上選擇性 Force 參數則可顯示隱藏或系統項目。例如,下列命令將顯示 Windows PowerShell 磁碟機 HKCU: (對應到 HKEY_CURRENT_USER 登錄 Hive) 根目錄中的項目:

PS> Get-ChildItem -Path hkcu:\


   Hive: Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER

SKC  VC Name                           Property
---  -- ----                           --------
  2   0 AppEvents                      {}
  7  33 Console                        {ColorTable00, ColorTable01, ColorTab...
 25   1 Control Panel                  {Opened}
  0   5 Environment                    {APR_ICONV_PATH, INCLUDE, LIB, TEMP...}
  1   7 Identities                     {Last Username, Last User ...
  4   0 Keyboard Layout                {}
...

在登錄編輯程式 (Regedit.exe) 中,這些項目都是 HKEY_CURRENT_USER 底下可見的最上層機碼。

您也可以將登錄路徑指定成登錄提供者的名稱,後面接著 "::"。登錄提供者的完整名稱為 Microsoft.PowerShell.Core\Registry,但可簡寫為 Registry。下列任何命令都將列出 HKCU 當層底下的內容:

Get-ChildItem -Path Registry::HKEY_CURRENT_USER
Get-ChildItem -Path Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER
Get-ChildItem -Path Registry::HKCU
Get-ChildItem -Path Microsoft.PowerShell.Core\Registry::HKCU
Get-ChildItem HKCU:

這些命令只會列出當層的內含項目,大致如同使用 Cmd.exe 的 DIR 命令或 UNIX 殼層的 ls 命令。若要顯示更內層所包含的項目,必須指定 Recurse 參數。例如,下列命令將列出 HKCU 底下所有的登錄機碼 (整個作業可能需要花費很長時間):

Get-ChildItem -Path hkcu:\ -Recurse

Get-ChildItem 可透過 PathFilterIncludeExclude 參數來執行複雜的篩選功能,但這些參數通常都是依名稱篩選。使用 Where-Object Cmdlet 則可依項目的其他屬性執行複雜的篩選作業。下列命令可找出 HKCU:\Software 底下,只包含一個以下的子機碼且剛好有四個值的所有機碼:

Get-ChildItem -Path HKCU:\Software -Recurse | Where-Object -FilterScript {($_.SubKeyCount -le 1) -and ($_.ValueCount -eq 4) }

複製機碼

使用 Copy-Item 可複製項目。下列命令將 HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion 及其屬性全部複製到 HKCU:\,進而建立新機碼 "CurrentVersion":

Copy-Item -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion' -Destination hkcu:

若您使用登錄編輯程式或 Get-ChildItem 檢查這個新機碼,會發現原本內含的子機碼並未複製到新位置。如需複製容器的所有內容,必須指定 Recurse 參數。下列命令將前項複製作業改為遞迴複製方式:

Copy-Item -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion' -Destination hkcu: -Recurse

您仍可使用其他既有的工具執行檔案系統複製作業。Windows PowerShell 能夠使用任何的登錄編輯工具 (包括 reg.exe、regini.exe 和 regedit.exe) 以及支援登錄編輯的 COM 物件 (諸如 WScript.Shell 和 WMI 的 StdRegProv 類別)。

建立機碼

在登錄中建立新機碼比起在檔案系統中建立新項目更加簡單。由於所有的登錄機碼都是容器,您不必指定項目類型;只要提供明確的路徑即可,例如:

New-Item -Path hkcu:\software\_DeleteMe

您也可以使用提供者型式的路徑來指定機碼:

New-Item -Path Registry::HKCU\_DeleteMe

刪除機碼

所有提供者刪除項目的做法基本上都一樣。下列命令將逕行移除項目而未出現提示:

Remove-Item -Path hkcu:\Software\_DeleteMe
Remove-Item -Path 'hkcu:\key with spaces in the name'

移除特定機碼底下所有的機碼

使用 Remove-Item 可移除內含項目,但如果項目中還包含其他項目,便會提示您確認是否全部移除。例如,若您嘗試刪除剛才建立的 HKCU:\CurrentVersion 子機碼,就會看到以下畫面:

Remove-Item -Path hkcu:\CurrentVersion

Confirm
The item at HKCU:\CurrentVersion\AdminDebug has children and the -recurse
parameter was not specified. If you continue, all children will be removed with
 the item. Are you sure you want to continue?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help
(default is "Y"):

若要跳過提示逕行刪除內含項目,請指定 -Recurse 參數:

Remove-Item -Path HKCU:\CurrentVersion -Recurse

如想移除 HKCU:\CurrentVersion 內所有的項目,但不移除 HKCU:\CurrentVersion 本身,則應改用下列命令:

Remove-Item -Path HKCU:\CurrentVersion\* -Recurse




目錄