Remove a registry key value
Description
This example shows how you can use the Registry
resource to ensure a registry key value doesn't
exist.
With Ensure set to Absent
, ValueName set to MyValue
, and Key set to
HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
, the resource removes the
MyValue
registry key value under the Environment
key if it exists.
With Invoke-DscResource
This script shows how you can use the Registry
resource with the Invoke-DscResource
cmdlet to
ensure the Environment
registry key doesn't have a value called MyValue
.
[CmdletBinding()]
param()
begin {
$SharedParameters = @{
Name = 'Registry'
ModuleName = 'PSDscResource'
Properties = @{
Key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
Ensure = 'Absent'
ValueName = 'MyValue'
}
}
$NonGetProperties = @(
'Ensure'
)
}
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 doesn't have a value called MyValue
.
Configuration RemoveValue {
Import-DscResource -ModuleName 'PSDscResources'
Node localhost {
Registry ExampleRegistry {
Key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
Ensure = 'Absent'
ValueName = 'MyValue'
}
}
}