Add or update a registry key value
Description
This example shows how you can use the Registry
resource to ensure a registry key value is set.
With Ensure set to Present
, ValueName set to MyValue
, and Key set to
HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
, the resource adds the
MyValue
registry key value under the Environment
key if it doesn't exist.
With ValueType set to Binary
, ValueData set to 0x00
, and Force set to $true
, the
resource sets the registry key value to 0
even if it exists with a different value.
With Invoke-DscResource
This script shows how you can use the Registry
resource with the Invoke-DscResource
cmdlet to
ensure the Environment
registry key has the MyValue
value set to 0
.
[CmdletBinding()]
param()
begin {
$SharedParameters = @{
Name = 'Registry'
ModuleName = 'PSDscResource'
Properties = @{
Key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
Ensure = 'Present'
ValueName = 'MyValue'
ValueType = 'Binary'
ValueData = '0x00'
Force = $true
}
}
$NonGetProperties = @(
'Ensure'
'ValueType'
'ValueData'
'Force'
)
}
process {
$TestResult = Invoke-DscResource -Method Test @SharedParameters
if ($TestResult.InDesiredState) {
$QueryParameters = $SharedParameters.Clone()
foreach ($Property in $NonGetProperties) {
$QueryParameters.Properties.Remove($Property)
}
Invoke-DscResource -Method Get @QueryParameters
} else {
Invoke-DscResource -Method Set @SharedParameters
}
}
With a Configuration
This snippet shows how you can define a Configuration
with a Registry
resource block to ensure
the Environment
registry key has the MyValue
value set to 0
.
Configuration AddOrModifyValue {
Import-DscResource -ModuleName 'PSDscResources'
Node localhost {
Registry ExampleRegistry {
Key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
Ensure = 'Present'
ValueName = 'MyValue'
ValueType = 'Binary'
ValueData = '0x00'
Force = $true
}
}
}
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for