Registry Editor (regedit.exe) และคำสั่ง reg.exe ไม่ใช่เครื่องมือเพียงอย่างเดียวสำหรับเข้าถึงและจัดการรีจิสทรีใน Windows PowerShell มีเครื่องมือมากมายสำหรับผู้ดูแลระบบในการโต้ตอบกับรีจิสทรี ใช้ PowerShell สร้าง แก้ไข ลบ registry key/parameter ค้นหาค่า และเชื่อมต่อกับรีจิสทรีบนคอมพิวเตอร์ระยะไกลได้
นำทาง Windows Registry เหมือน File System ด้วย PowerShell
การทำงานกับรีจิสทรีใน PowerShell คล้ายกับการจัดการไฟล์ทั่วไปบนดิสก์ท้องถิ่น ความแตกต่างคือ Registry Key เทียบเท่าไฟล์ ส่วน Registry Parameter คือคุณสมบัติของไฟล์เหล่านั้น
แสดงรายการไดรฟ์ที่มีอยู่:
get-psdrive

สังเกตว่าระหว่างไดรฟ์ (ที่มีตัวอักษรไดรฟ์) มีอุปกรณ์พิเศษผ่าน Registry Provider คือ HKCU (HKEY_CURRENT_USER) และ HKLM (HKEY_LOCAL_MACHINE) สามารถเรียกดูโครงสร้างรีจิสทรีได้เหมือนการนำทางไดรฟ์ ใช้ HKLM:\ และ HKCU:\ เพื่อเข้าถึง Registry Hive เฉพาะ
cd HKLM:\
Dir -ErrorAction SilentlyContinue

ดังนั้น สามารถเข้าถึง Registry Key และ Parameter ด้วย Cmdlets เดียวกับที่จัดการไฟล์/โฟลเดอร์
สำหรับ Registry Key ใช้ Cmdlets กลุ่ม xxx-Item:
Get-Item– ดึง Registry KeyNew-Item— สร้าง Registry Key ใหม่Remove-Item– ลบ Registry Key
Registry Parameter ถือเป็น Property ของ Registry Key (คล้ายคุณสมบัติไฟล์/โฟลเดอร์) ใช้ Cmdlets กลุ่ม xxx-ItemProperty:
Get-ItemProperty– ดึงค่า Registry ParameterSet-ItemProperty– เปลี่ยนค่า Registry ParameterNew-ItemProperty– สร้าง Registry ParameterRename-ItemProperty– เปลี่ยนชื่อ ParameterRemove-ItemProperty— ลบ Registry Parameter
นำทางไปยัง Registry Key เฉพาะ (เช่น Key ที่ควบคุมการอัปเดตไดรเวอร์อัตโนมัติ) ด้วยคำสั่งใดคำสั่งหนึ่ง:
cd HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DriverSearching
หรือ
Set-Location -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DriverSearching
ดึงค่า Registry Parameter ผ่าน PowerShell
โปรดทราบว่า Parameter ที่เก็บใน Registry Key ไม่ใช่ออบเจ็กต์ซ้อนกัน แต่เป็น Property ของ Key นั้นๆ Key ใดก็ตามสามารถมี Parameter ได้หลายตัว
แสดงเนื้อหาของ Registry Key ปัจจุบัน:
dir หรือ Get-ChildItem
คำสั่งแสดงข้อมูล Subkey และ Property แต่ไม่แสดง Parameter SearchOrderConfig ซึ่งเป็น Property ของ Key ปัจจุบัน
ใช้ Get-Item เพื่อดู Parameter ของ Registry Key:
Get-Item .
หรือ
Get-Item –Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DriverSearching
Key DriverSearching มี Parameter เดียวคือ SearchOrderConfig ค่า 1

ดึงค่า Parameter ใช้ Get-ItemProperty:
$DriverUpdate = Get-ItemProperty –Path ‘HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DriverSearching’
$DriverUpdate.SearchOrderConfig

ได้ค่า SearchOrderConfig = 1
เปลี่ยนค่า Registry ด้วย PowerShell
เปลี่ยนค่า SearchOrderConfig ใช้ Set-ItemProperty:
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DriverSearching' -Name SearchOrderConfig -Value 0
ตรวจสอบว่าค่าเปลี่ยนแล้ว:
Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\DriverSearching' -Name SearchOrderConfig

สร้าง Registry Key หรือ Parameter ใหม่ด้วย PowerShell
สร้าง Key ใหม่ ใช้ New-Item สร้าง Key ชื่อ NewKey:
$HKCU_Desktop= "HKCU:\Control Panel\Desktop"
New-Item –Path $HKCU_Desktop –Name NewKey
สร้าง Parameter ใหม่ใน Key ใหม่ สมมติต้องการสร้าง String Parameter ชนิด REG_SZ ชื่อ SuperParamString ค่า filetmp1.txt:
New-ItemProperty -Path $HKCU_Desktop\NewKey -Name "SuperParamString" -Value ”filetmp1.txt” -PropertyType "String"
ชนิดข้อมูลที่ใช้ได้สำหรับ Registry Parameter:
- String (REG_SZ)
- ExpandString (REG_EXPAND_SZ)
- MultiString (REG_MULTI_SZ)
- Binary (REG_BINARY)
- DWord (REG_DWORD)
- Qword (REG_QWORD)
- Unknown (ชนิดข้อมูลที่ไม่รองรับ)
ตรวจสอบว่า Key และ Parameter ใหม่ปรากฏในรีจิสทรีแล้ว

วิธีตรวจสอบว่า Registry Key มีอยู่จริง?
ใช้ Test-Path:
Test-Path 'HKCU:\Control Panel\Desktop\NewKey'
สคริปต์ PowerShell ตรวจสอบว่า Registry Value มีอยู่หรือไม่ ถ้าไม่มีให้สร้าง:
regkey='HKCU:\Control Panel\Desktop\NewKey'
$regparam='testparameter'
if (Get-ItemProperty -Path $regkey -Name $regparam -ErrorAction Ignore)
{ write-host 'The registry entry already exist' }
else
{ New-ItemProperty -Path $regkey -Name $regparam -Value ”woshub_test” -PropertyType "String" }
ใช้ Copy-Item คัดลอก Entry จาก Key หนึ่งไปอีก Key:
$source='HKLM:\SOFTWARE\7-zip\'
$dest = 'HKLM:\SOFTWARE\backup'
Copy-Item -Path $source -Destination $dest -Recurse
เพื่อคัดลอกรวม Subkey ให้เพิ่มสวิตช์ –Recurse
ลบ Registry Key หรือ Parameter
ใช้ Remove-ItemProperty ลบ Parameter ใน Registry Key ลบ Parameter SuperParamString ที่สร้างไว้ก่อนหน้า:
$HKCU_Desktop= "HKCU:\Control Panel\Desktop"
Remove-ItemProperty –Path $HKCU_Desktop\NewKey –Name "SuperParamString"
ลบ Registry Key ทั้งหมดพร้อมเนื้อหา:
Remove-Item –Path $HKCU_Desktop\NewKey –Recurse
หมายเหตุ สวิตช์ –Recurse ระบุว่าต้องลบ Subkey ทั้งหมดแบบ Recursive
ลบรายการทั้งหมดใน Key (แต่ไม่ลบ Key เอง):
Remove-Item –Path $HKCU_Desktop\NewKey\* –Recurse
เปลี่ยนชื่อ Registry Key หรือ Parameter
เปลี่ยนชื่อ Registry Parameter:
Rename-ItemProperty –path ‘HKCU:\Control Panel\Desktop\NewKey’ –name "SuperParamString" –newname “OldParamString”
เปลี่ยนชื่อ Registry Key แบบเดียวกัน:
Rename-Item -path 'HKCU:\Control Panel\Desktop\NewKey' OldKey
ค้นหา Registry ด้วยคีย์เวิร์ดผ่าน PowerShell
PowerShell ค้นหา Registry ได้ ตัวอย่างค้นหาใน HKCU:\Control Panel\Desktop Parameter ที่ชื่อมี *dpi*:
$Path = (Get-ItemProperty ‘HKCU:\Control Panel\Desktop’)
$Path.PSObject.Properties | ForEach-Object {
If($_.Name -like '*dpi*'){
Write-Host $_.Name ' = ' $_.Value
}
}
ค้นหา Registry Key ด้วยชื่อเฉพาะ:
Get-ChildItem -path HKLM:\ -recurse -ErrorAction SilentlyContinue | Where-Object {$_.Name -like "*woshub*"}
ตั้งค่าสิทธิ์ Registry Key ด้วย PowerShell
ดูสิทธิ์ปัจจุบันของ Registry Key ด้วย Get-ACL (Get-ACL ยังจัดการสิทธิ์ NTFS บนไฟล์/โฟลเดอร์ได้อีกด้วย)
$rights = Get-Acl -Path 'HKCU:\Control Panel\Desktop\NewKey'
$rights.Access.IdentityReference

ตัวอย่าง: แก้ไข ACL ของ Key นี้ ให้สิทธิ์เขียนแก่กลุ่ม Built-in Users
ดูสิทธิ์ปัจจุบัน: $rights = Get-Acl -Path 'HKCU:\Control Panel\Desktop\NewKey'
ระบุผู้ใช้/กลุ่มที่ต้องการให้สิทธิ์: $idRef = [System.Security.Principal.NTAccount]"BuiltIn\Users"
เลือกระดับการเข้าถึง: $regRights = [System.Security.AccessControl.RegistryRights]::WriteKey
ตั้งค่าการสืบทอดสิทธิ์: $inhFlags = [System.Security.AccessControl.InheritanceFlags]::None; $prFlags = [System.Security.AccessControl.PropagationFlags]::None
ประเภทการเข้าถึง (Allow/Deny): $acType = [System.Security.AccessControl.AccessControlType]::Allow
สร้าง Access Rule: $rule = New-Object System.Security.AccessControl.RegistryAccessRule ($idRef, $regRights, $inhFlags, $prFlags, $acType)
เพิ่ม Rule ใหม่ใน ACL ปัจจุบัน: $rights.AddAccessRule($rule)
นำสิทธิ์ใหม่ไปใช้กับ Registry Key: $rights | Set-Acl -Path 'HKCU:\Control Panel\Desktop\NewKey'
ตรวจสอบว่ากลุ่มใหม่ปรากฏใน ACL แล้ว

ดึงค่า Registry จากคอมพิวเตอร์ระยะไกลผ่าน PowerShell
PowerShell เข้าถึง Registry ของคอมพิวเตอร์ระยะไกลได้ เชื่อมต่อผ่าน WinRM (Invoke-Command หรือ Enter-PSSession) ดึงค่า Parameter จากคอมพิวเตอร์ระยะไกล:
Invoke-Command –ComputerName srv-fs1 –ScriptBlock {Get-ItemProperty -Path 'HKLM:\System\Setup' -Name WorkingDirectory}
หรือใช้การเชื่อมต่อ Remote Registry (ต้องเปิดใช้งานบริการ RemoteRegistry)
$Server = "lon-fs1"
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Server)
$RegKey= $Reg.OpenSubKey("System\Setup")
$RegValue = $RegKey.GetValue("WorkingDirectory")
เคล็ดลับ หากต้องสร้าง/แก้ไข Registry Parameter บนคอมพิวเตอร์โดเมนจำนวนมาก ใช้ฟีเจอร์ GPO จะง่ายกว่า
บทความนี้ครอบคลุมตัวอย่างทั่วไปของการใช้ PowerShell เข้าถึงและจัดการ Registry ใช้ได้ในสคริปต์อัตโนมัติของคุณ