简体   繁体   中英

Set a hex registry value with PowerShell

I have a Registration Entries (.reg) file, and I wanted to convert it to a PowerShell script.

On my way, I encountered this value: hex:00 .

Here is the registry key & value that I want to set:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache\Tasks\{042D8A51-5878-4000-9C10-C04AFF122A1F}"

"Triggers"=hex:00

How do I set this Hex value using Set-ItemPropery?

When you use Set-ItemProperty to target registry paths, the cmdlet supports a dynamic parameter named -Type that accepts a Microsoft.Win32.RegistryValueKind value, which specifies the value's data type.

The presence of hex: in your *.reg file implies binary (raw bytes) as the data type; therefore:

  • pass Binary to -Type
  • pass the binary value (data) as an array of bytes to -Value ; to produce the equivalent of hex:00 - ie a single byte with value 0x0 - use -Value 0x0 (to pass multiple bytes, separate them with , eg: -Value 0x0, 0x1 ):
Set-ItemProperty -Type Binary -Value 0x0 -Name Triggers -LiteralPath 'registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache\Tasks\{042D8A51-5878-4000-9C10-C04AFF122A1F}'

Also note the registry:: prefix to the registry key path, which is required to identify the path as a registry path (in a context-independent manner).

Alternatively, replace registry::HKEY_LOCAL_MACHINE with HKLM: , to base the path on the equivalent PowerShell-specific drive instead. (The other predefined registry drive is HKCU: , which is equivalent to registry::HKEY_CURRENT_USER ; Get-PSDrive -PSProvider registry shows all registry-based drives).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM