Get-ItemProperty and Set-ItemProperty are most useful when manipulating registry values.
The following method may be used to get values from the registry:
Get-ItemProperty -Path HKCU:Environment Get-ItemProperty -Path HKCU:Environment -Name Path Get-ItemProperty -Path HKCU:Environment -Name Path, Temp
Individual values may be written back to the registry under an existing key:
Set-ItemProperty -Path HKCU:Environment -Name NewValue -Value 'New'
A value may be subsequently removed:
Remove-ItemProperty -Path HKCU:Environment -Name NewValue
The Set-ItemProperty command does not directly allow the value type to be influenced. The command will do as much as it can to fit the value into the existing type. For a property with type REG_SZ, numbers will be converted to strings.
If a value does not already exist, a registry type will be created according to the value type:
- Int32: REG_DWORD
- Int64: REG_QWORD
- String: REG_SZ
- String[]: REG_MULTI_SZ (must use "[String[]]@('value', 'value')")
- Byte[]: REG_BINARY
- Any other type: REG_SZ
If a value of a specific type is required, the New-ItemProperty command should be used instead, for instance; if an expanding string must be created:
New-ItemProperty HKCU:Environment -Name Expand -Value 'User: %USERNAME%' -PropertyType ExpandString
New-ItemProperty will throw an error if a property already exists. The Force parameter may be used to overwrite an existing value with the same name.