Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Description
This example shows how you can use the Registry resource to ensure a registry key doesn't exist.
With Ensure set to Absent, ValueName set to an empty string, and Key set to
HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\MyNewKey, the resource removes
the MyNewKey registry 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 MyNewKey registry key doesn't exist.
[CmdletBinding()]
param()
begin {
$SharedParameters = @{
Name = 'Registry'
ModuleName = 'PSDscResource'
Properties = @{
Key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\MyNewKey'
Ensure = 'Absent'
ValueName = ''
}
}
$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 MyNewKey registry key doesn't exist.
Configuration RemoveKey {
Import-DscResource -ModuleName 'PSDscResources'
Node localhost {
Registry ExampleRegistry {
Key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\MyNewKey'
Ensure = 'Absent'
ValueName = ''
}
}
}