-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRemove-RegistryKeyValue.ps1
More file actions
89 lines (84 loc) · 4.24 KB
/
Copy pathRemove-RegistryKeyValue.ps1
File metadata and controls
89 lines (84 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function Remove-RegistryKeyValue {
[CmdletBinding(SupportsShouldProcess=$True,
ConfirmImpact='Medium',
HelpURI='http://vcloud-lab.com',
DefaultParameterSetName='DelValue')]
Param (
[parameter(ParameterSetName = 'DelValue', Position=0, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[parameter(ParameterSetName = 'DelKey', Position=0, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[alias('C')]
[String[]]$ComputerName = '.',
[Parameter(ParameterSetName = 'DelValue', Position=1, Mandatory=$True, ValueFromPipelineByPropertyName=$True)]
[parameter(ParameterSetName = 'DelKey', Position=1, ValueFromPipelineByPropertyName=$True)]
[alias('Hive')]
[ValidateSet('ClassesRoot', 'CurrentUser', 'LocalMachine', 'Users', 'CurrentConfig')]
[String]$RegistryHive = 'LocalMachine',
[Parameter(ParameterSetName = 'DelValue', Position=2, Mandatory=$True, ValueFromPipelineByPropertyName=$True)]
[parameter(ParameterSetName = 'DelKey', Position=2, ValueFromPipelineByPropertyName=$True)]
[alias('ParentKeypath')]
[String]$RegistryKeyPath,
[parameter(ParameterSetName = 'DelKey',Position=3, Mandatory=$True, ValueFromPipelineByPropertyName=$true)]
[String[]]$ChildKey,
[parameter(ParameterSetName = 'DelValue',Position=5, Mandatory=$True, ValueFromPipelineByPropertyName=$true)]
[String[]]$ValueName
)
Begin {
$RegistryRoot= "[{0}]::{1}" -f 'Microsoft.Win32.RegistryHive', $RegistryHive
try {
$RegistryHive = Invoke-Expression $RegistryRoot -ErrorAction Stop
}
catch {
Write-Host "Incorrect Registry Hive mentioned, $RegistryHive does not exist"
}
}
Process {
Foreach ($Computer in $ComputerName) {
if (Test-Connection $Computer -Count 2 -Quiet) {
try {
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($RegistryHive, $Computer)
$key = $reg.OpenSubKey($RegistryKeyPath, $true)
}
catch {
Write-Host "Check permissions on computer name $Computer, cannot connect registry" -BackgroundColor DarkRed
Continue
}
switch ($PsCmdlet.ParameterSetName) {
'DelValue' {
foreach ($regvalue in $ValueName) {
if ($key.GetValueNames() -contains $regvalue) {
[void]$key.DeleteValue($regvalue)
}
else {
Write-Host "Registry value name $regvalue doesn't exist on Computer $Computer under path $RegistryKeyPath" -BackgroundColor DarkRed
}
}
break
}
'DelKey' {
foreach ($regkey in $ChildKey) {
if ($key.GetSubKeyNames() -contains $regkey) {
[void]$Key.DeleteSubKey("$regkey")
}
else {
Write-Host "Registry key $regKey doesn't exist on Computer $Computer under path $RegistryKeyPath" -BackgroundColor DarkRed
}
}
break
}
}
}
else {
Write-Host "Computer Name $Computer not reachable" -BackgroundColor DarkRed
}
}
}
End {
#[Microsoft.Win32.RegistryHive]::ClassesRoot
#[Microsoft.Win32.RegistryHive]::CurrentUser
#[Microsoft.Win32.RegistryHive]::LocalMachine
#[Microsoft.Win32.RegistryHive]::Users
#[Microsoft.Win32.RegistryHive]::CurrentConfig
}
}
#Remove-RegistryKeyValue -ComputerName server01, member01 -RegistryHive LocalMachine -RegistryKeyPath SYSTEM\DemoKey -ChildKey test1, test2
#Remove-RegistryKeyValue -ComputerName server01, member01 -RegistryHive LocalMachine -RegistryKeyPath SYSTEM\DemoKey -ValueName start, exp