Registry/Registry.ps1

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
<#
.Synopsis
   Enumerates the keys that are under a given Registry Key.
.DESCRIPTION
   Enumerates the keys that are under a given Registry Key.
.EXAMPLE
   Listing all of the keys under HKCU\Software key
   PS C:\> Get-RegKeys -Hive HKCU -Key software

    Key FullPath
    --- --------
    7-Zip HKCU\software\7-Zip
    AppDataLow HKCU\software\AppDataLow
    Macromedia HKCU\software\Macromedia
    Microsoft HKCU\software\Microsoft
    Microsoft Corporation HKCU\software\Microsoft Corporation
    Mine HKCU\software\Mine
    Policies HKCU\software\Policies
    RegisteredApplications HKCU\software\RegisteredApplications
    ThinPrint HKCU\software\ThinPrint
    VMware, Inc. HKCU\software\VMware, Inc.
    Wow6432Node HKCU\software\Wow6432Node
    Classes HKCU\software\Classes
#>

function Get-RegKeys
{
    [CmdletBinding()]
    Param
    (
        [parameter(Mandatory=$true)]
        [string]
        [Validateset(“HKCR”, “HKCU”, “HKLM”, "HKUS", "HKCC")]
        $Hive,

        [parameter(Mandatory=$true)]
        [AllowEmptyString()]
        [string]$Key,
 
        [parameter(ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
        [string]$Computername="$env:COMPUTERNAME",

        [Parameter(Mandatory=$false)]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty
    )

    Begin
    {
        $reg = Get-WmiObject -List "StdRegprov" -ComputerName $computername -Credential $Credential
    }
    Process
    {
        # We get the correct value associated with the key.
        switch ($hive) 
        {
            "HKCR" {$reg_hive = 2147483648}
            "HKCU" {$reg_hive = 2147483649}
            "HKLM" {$reg_hive = 2147483650}
            "HKUS" {$reg_hive = 2147483651}
            "HKCC" {$reg_hive = 2147483653}
        }

        $data = $reg.EnumKey($reg_hive, $key)
        if ($data.ReturnValue -eq 0)
        {
            $keynum = ($data.snames).Length
            if ($keynum -gt 0) 
            {
                foreach($keyname in $data.snames)
                {
                    New-Object PSObject -Property @{Key=$keyname;FullPath = "$hive\$key\$keyname"}
                } 
            }
            else
            {
                Write-Verbose "Key $key does not have any keys to enumerate"
            }
        }
        elseif ($data.ReturnValue -eq 2)
        {
            Write-Error "Key $key does not exist"
        }
        else
        {
            Write-Error "Error when enumerating keys: $($data.ReturnValue)"
        }
    }
    End
    {
    }
}


<#
.Synopsis
   Creates a registry key under a given Registry Key.
.DESCRIPTION
   Creates a registry key under a given Registry Key.
.EXAMPLE
   Create a key named _deleteme under the registry HKCU key.
   PS C:\> New-RegKey -Hive HKCU -Key _deleteme -Verbose
   VERBOSE: Key HKCU\_deleteme was created.
#>

function New-RegKey
{
    [CmdletBinding()]
    Param
    (
        [parameter(Mandatory=$true)]
        [string]
        [Validateset(“HKCR”, “HKCU”, “HKLM”, "HKUS", "HKCC")]
        $Hive,

        [parameter(Mandatory=$true)]
        [string]$Key,
 
        [parameter(ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
        [string]$Computername="$env:COMPUTERNAME",

        [Parameter(Mandatory=$false)]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty
    )

    Begin
    {
        $reg = Get-WmiObject -List "StdRegprov" -ComputerName $computername -Credential $Credential
    }
    Process
    {
        # We get the correct value associated with the key.
        switch ($hive) 
        {
            "HKCR" {$reg_hive = 2147483648}
            "HKCU" {$reg_hive = 2147483649}
            "HKLM" {$reg_hive = 2147483650}
            "HKUS" {$reg_hive = 2147483651}
            "HKCC" {$reg_hive = 2147483653}
        }

        $data = $reg.CreateKey($reg_hive, $key)
        if ($data.ReturnValue -eq 0)
        {
            Write-Verbose "Key $hive\$key was created."
        }
        elseif ($data.ReturnValue -eq 2)
        {
            Write-Error "Key $key does not exist"
        }
        else
        {
            Write-Error "Error when creating key: $($data.ReturnValue)"
        }
    }
    End
    {
    }
}


<#
.Synopsis
   Removes a registry key under a given Registry Key.
.DESCRIPTION
   Removes a registry key under a given Registry Key.
.EXAMPLE
   Remove a key named _deleteme under the registry HKCU key.
   PS C:\> Remove-RegKey -Hive HKCU -Key _deleteme -Verbose
   VERBOSE: Key HKCU\_deleteme was removed.
#>

function Remove-RegKey
{
    [CmdletBinding()]
    Param
    (
        [parameter(Mandatory=$true)]
        [string]
        [Validateset(“HKCR”, “HKCU”, “HKLM”, "HKUS", "HKCC")]
        $Hive,

        [parameter(Mandatory=$true)]
        [string]$Key,
 
        [parameter(ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
        [string]$Computername="$env:COMPUTERNAME",

        [Parameter(Mandatory=$false)]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty
    )

    Begin
    {
        $reg = Get-WmiObject -List "StdRegprov" -ComputerName $computername -Credential $Credential
    }
    Process
    {
        # We get the correct value associated with the key.
        switch ($hive) 
        {
            "HKCR" {$reg_hive = 2147483648}
            "HKCU" {$reg_hive = 2147483649}
            "HKLM" {$reg_hive = 2147483650}
            "HKUS" {$reg_hive = 2147483651}
            "HKCC" {$reg_hive = 2147483653}
        }

        $data = $reg.DeleteKey($reg_hive, $key)
        if ($data.ReturnValue -eq 0)
        {
            Write-Verbose "Key $hive\$key was removed."
        }
        elseif ($data.ReturnValue -eq 2)
        {
            Write-Error "Key $key does not exist"
        }
        else
        {
            Write-Error "Error when removing key: $($data.ReturnValue)"
        }
    }
    End
    {
    }
}


<#
.Synopsis
   Enumerates the values and their type of a given Registry Key.
.DESCRIPTION
   Enumerates the values and their type of a given Registry Key.
.EXAMPLE
   Listing all of the console settings in the HKCU\Console key
   Get-RegValues -Hive HKCU -Key console
#>

function Get-RegValues
{
    [CmdletBinding()]
    Param
    (
        [parameter(Mandatory=$true)]
        [string]
        [Validateset(“HKCR”, “HKCU”, “HKLM”, "HKUS", "HKCC")]
        $Hive,

        [parameter(Mandatory=$true)]
        [string]$Key,
 
        [parameter(ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
        [string]$Computername="$env:COMPUTERNAME",

        [Parameter(Mandatory=$false)]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty
    )

    Begin
    {
        $reg = Get-WmiObject -List "StdRegprov" -ComputerName $computername -Credential $Credential
    }
    Process
    {
        # We get the correct value associated with the key.
        switch ($hive) 
        {
            "HKCR" {$reg_hive = 2147483648}
            "HKCU" {$reg_hive = 2147483649}
            "HKLM" {$reg_hive = 2147483650}
            "HKUS" {$reg_hive = 2147483651}
            "HKCC" {$reg_hive = 2147483653}
        }

        # Registry value types
        $reg_types = @{
            "1" = "REG_SZ"
            "2" = "REG_EXPAND_SZ"
            "3" = "REG_BINARY"
            "4" = "REG_DWORD"
            "7" = "REG_MULTI_SZ"
            "11" = "REG_QWORD"
        }

        $data = $reg.EnumValues($reg_hive, $key)
        if ($data.ReturnValue -eq 0)
        {
            $keynum = ($data.snames).Length
            if ($keynum -gt 0) 
            {
                for ($i=0; $i -le $keynum; $i++)
                {
                    New-Object PSObject -Property @{ValueName="$($data.snames[$i])";Type = $reg_types["$($data.types[$i])"]}
                }
            }
            else
            {
                Write-Verbose "Key $key does not have any values to enumerate"
            }
        }
        elseif ($data.ReturnValue -eq 2)
        {
            Write-Error "Key $key does not exist"
        }
        else
        {
            Write-Error "Error when enumerating values: $($data.ReturnValue)"
        }
    }
    End
    {
    }
}


<#
.Synopsis
   Checks for specific access type on a given registry key.
.DESCRIPTION
   Checks for specific access type on a given registry key. Returns a Boolean value.
.PARAMETER AccessType
   Access Type to test against a given Registry Key

   - KEY_QUERY_VALUE Required to query the values of a registry key.
   - KEY_SET_VALUE Required to create, delete, or set a registry value.
   - KEY_CREATE_SUB_KEY Required to create a subkey of a registry key.
   - KEY_ENUMERATE_SUB_KEYS Required to enumerate the subkeys of a registry key.
   - KEY_NOTIFY Required to request change notifications for a registry key or for subkeys of a registry key.
   - KEY_CREATE_SUB_KEY Required to create a registry key.
   - DELETE Required to delete a registry key.
   - READ_CONTROL Combines the STANDARD_RIGHTS_READ, KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS, and KEY_NOTIFY values.
   - WRITE_DAC Required to modify the DACL in the object's security descriptor.
   - WRITE_OWNER Required to change the owner in the object's security descriptor.
.EXAMPLE
   PS C:\> Test-RegKeyAccess -Hive hkcu -Key software -AccessType DELETE
   True
#>

function Test-RegKeyAccess
{
    [CmdletBinding()]
  
    Param
    (
        [parameter(Mandatory=$true)]
        [string]
        [Validateset(“HKCR”, “HKCU”, “HKLM”, "HKUS", "HKCC")]
        $Hive,

        [parameter(Mandatory=$true)]
        [string]
        [Validateset(“KEY_QUERY_VALUE”, "KEY_CREATE_SUB_KEY", "KEY_ENUMERATE_SUB_KEYS", 
        "KEY_NOTIFY", "KEY_CREATE", "DELETE", "READ_CONTROL", "WRITE_DAC", "WRITE_OWNER")]
        $AccessType,

        [parameter(Mandatory=$true)]
        [string]$Key,
 
        [parameter(ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
        [string]$Computername="$env:COMPUTERNAME",

        [Parameter(Mandatory=$false)]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty
    )

    Begin
    {
        $reg = Get-WmiObject -List "StdRegprov" -ComputerName $computername -Credential $Credential
    }
    Process
    {
        # We get the correct value associated with the key.
        switch ($hive) 
        {
            "HKCR" {$reg_hive = 2147483648}
            "HKCU" {$reg_hive = 2147483649}
            "HKLM" {$reg_hive = 2147483650}
            "HKUS" {$reg_hive = 2147483651}
            "HKCC" {$reg_hive = 2147483653}
        }

        switch ($AccessType)
        {
            “KEY_QUERY_VALUE”        {$type2check = 1}
            "KEY_SET_VALUE"          {$type2check = 2}
            "KEY_CREATE_SUB_KEY"     {$type2check = 4}
            "KEY_ENUMERATE_SUB_KEYS" {$type2check = 8}
            "KEY_NOTIFY"             {$type2check = 16}
            "KEY_CREATE"             {$type2check = 32}
            "DELETE"                 {$type2check = 65536}
            "READ_CONTROL"           {$type2check = 131072}
            "WRITE_DAC"              {$type2check = 262144}
            "WRITE_OWNER"            {$type2check = 524288}
        }

        $data = $reg.CheckAccess($reg_hive, $key, $type2check)
        if ($data.ReturnValue -eq 0)
        {
           $data.bGranted
        }
        elseif ($data.ReturnValue -eq 2)
        {
            Write-Error "Key $key does not exist"
        }
        else
        {
            Write-Error "Error when Checking Access Type on key: $($data.ReturnValue)"
        }
    }
    End
    {
    }
}


<#
.Synopsis
   Set a value on a given key.
.DESCRIPTION
   Set a value on a given key using WMI.
.EXAMPLE
   set a SZ value.
   PS C:\> Set-RegValue -Hive HKCU -Key _deleteme -Name stringval -Type SZ -Data "data" -Verbose
   VERBOSE: Value set on HKCU\_deletme\stringval of type SZ
.EXAMPLE
   Set a MULTISZ value.
   PS C:\> Set-RegValue -Hive HKCU -Key _deleteme -Name multistring -Type MULTISZ -Data "str1","str2","str3" -Verbose
   VERBOSE: Value set on HKCU\_deletme\multistring of type MULTISZ
.EXAMPLE
   Set a QWORD value.
   PS C:\> Set-RegValue -Hive HKCU -Key _deleteme -Name qval -Type QWORD -Data 4060 -Verbose
   VERBOSE: Value set on HKCU\_deletme\qval of type QWORD
.EXAMPLE
   Set a EXPANDSZ value.
   PS C:\> Set-RegValue -Hive HKCU -Key _deleteme -Name expanval -Type EXPANDSZ -Data "%envvar%" -Verbose
   VERBOSE: Value set on HKCU\_deletme\expanval of type EXPANDSZ
.EXAMPLE
   Set a DWORD value.
   PS C:\> Set-RegValue -Hive HKCU -Key _deleteme -Name dworval -Type DWORD -Data 10 -Verbose
   VERBOSE: Value set on HKCU\_deletme\dworval of type DWORD
.EXAMPLE
   Set a Binary value.
   PS C:\> Set-RegValue -Hive HKCU -Key _deleteme -Name binval -Type BINARY -Data @([char[]]'PowerShell') -Verbose
   VERBOSE: Value set on HKCU\_deletme\binval of type BINARY
#>

function Set-RegValue
{
    [CmdletBinding()]
    
    Param
    (
        [parameter(Mandatory=$true)]
        [Validateset(“HKCR”, “HKCU”, “HKLM”, "HKUS", "HKCC")]
        [string]$Hive,

        [parameter(Mandatory=$true)]
        [Validateset(“DWORD”, “EXPANDSZ”, “MULTISZ”, "QWORD", "SZ", "BINARY")]
        [string]$Type,

        [parameter(Mandatory=$true)]
        [string]$Key,

        [parameter(Mandatory=$true)]
        [string]$Name,

        [parameter(Mandatory=$true)]
        $Data,
 
        [parameter(ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
        [string]$Computername="$env:COMPUTERNAME",

        [Parameter(Mandatory=$false)]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty
    )

    Begin
    {
        $reg = Get-WmiObject -List "StdRegprov" -ComputerName $computername -Credential $Credential
        
    }
    Process
    {
        # We get the correct value associated with the key.
        switch ($hive) 
        {
            "HKCR" {$reg_hive = 2147483648}
            "HKCU" {$reg_hive = 2147483649}
            "HKLM" {$reg_hive = 2147483650}
            "HKUS" {$reg_hive = 2147483651}
            "HKCC" {$reg_hive = 2147483653}
        }

        # Set according to type
        switch ($type) 
        {
            “DWORD”     {$data = ($reg.SetDwordValue($reg_hive, $key, $Name, $Data))}
            “EXPANDSZ”  {$data = ($reg.SetExpandedStringValue($reg_hive, $key, $Name, $Data))}
            “MULTISZ”   {$data = ($reg.SetMultiStringValue($reg_hive, $key, $Name, $Data))}
            "QWORD"     {$data = ($reg.SetQwordValue($reg_hive, $key, $Name, $Data))}
            "SZ"        {$data = ($reg.SetStringValue($reg_hive, $key, $Name, $Data))}
            "BINARY"    {$data = ($reg.SetBinaryValue($reg_hive, $key, $Name, $Data))}
        }

        # process return value
        if ($data.ReturnValue -eq 0)
        {
           Write-Verbose "Value set on $hive\$key\$name of type $type"
        }
        elseif ($data.ReturnValue -eq 2)
        {
            Write-Error "Key $key does not exist"
        }
        else
        {
            Write-Error "Error when setting value on key: $($data.ReturnValue)"
        }
    }
    End
    {
    }
}


<#
.Synopsis
   Retrives a the content of a specified value in a given key.
.DESCRIPTION
   Retrives a the content of a specified value in a given key.
.EXAMPLE
   Getting the Windows Version fromt the registry
   PS C:\> Get-RegValue -Hive HKLM -key "SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName
   Windows 8 Enterprise
.EXAMPLE
    Read registry binary data and turn it in to ASCII String
    PS C:\> $bindata = Get-RegValue -Hive HKCU -Key _deleteme -Name binval
    PS C:\> ([System.Text.Encoding]::ASCII).GetString($bindata)
    PowerShell

PS C:\Wind
#>

function Get-RegValue
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        [parameter(Mandatory=$true)]
        [Validateset(“HKCR”, “HKCU”, “HKLM”, "HKUS", "HKCC")]
        [string]$Hive,

        [parameter(Mandatory=$true)]
        [string]$Key,

        [parameter(Mandatory=$true)]
        [string]$Name,
 
        [parameter(ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
        [string]$Computername="$env:COMPUTERNAME",

        [Parameter(Mandatory=$false)]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty
    )

    Begin
    {
        $reg = Get-WmiObject -List "StdRegprov" -ComputerName $computername -Credential $Credential
    }
    Process
    {
        # We get the correct value associated with the key.
        switch ($hive) 
        {
            "HKCR" {$reg_hive = 2147483648}
            "HKCU" {$reg_hive = 2147483649}
            "HKLM" {$reg_hive = 2147483650}
            "HKUS" {$reg_hive = 2147483651}
            "HKCC" {$reg_hive = 2147483653}
        }

        $valdata = $reg.EnumValues($reg_hive, $key)
        if ($valdata.returnvalue -eq 0) 
        {
            # check that the value actualy exists
            if ($valdata.snames -contains $Name) 
            {
                # Get value index in the array
                $index = (0..($valdata.snames.Count - 1) | Where { $valdata.snames[$_] -eq $Name })
                $type = $valdata.types[$index]
                # Get according to type
                switch ($type) 
                {
                    “4”  {$data = ($reg.GetDwordValue($reg_hive, $key, $Name)).uvalue}
                    “2”  {$data = ($reg.GetExpandedStringValue($reg_hive, $key, $Name)).svalue}
                    “7”  {$data = ($reg.GetMultiStringValue($reg_hive, $key, $Name)).svalue}
                    "11" {$data = ($reg.GetQwordValue($reg_hive, $key, $Name)).uvalue}
                    "1"  {$data = ($reg.GetStringValue($reg_hive, $key, $Name)).svalue}
                    "3"  {$data = ($reg.GetBinaryValue($reg_hive, $key, $Name)).uvalue}
                }
                $data
            }
            else
            {
                Write-Error "Value $name does not exist in key specified."
            }
        }
        elseif ($valdata.returnvalue -eq 2)
        {
            Write-Error "Key $key does not exist"
        }
        else
        {
            Write-Error "Error when retreaving value on key: $($data.ReturnValue)"
        }
    }
    End
    {
    }
}


<#
.Synopsis
   Removes a specified value in a given key.
.DESCRIPTION
   Removes a specified value in a given key.
.EXAMPLE
   Removing a value from the registry
   PS C:\> Remove-RegValue -Hive HKCU -Key _deleteme -Name dworval -Verbose
   VERBOSE: Value dworval has been removed.
#>

function Remove-RegValue
{
    [CmdletBinding()]
    [OutputType([int])]
    Param
    (
        [parameter(Mandatory=$true)]
        [Validateset(“HKCR”, “HKCU”, “HKLM”, "HKUS", "HKCC")]
        [string]$Hive,

        [parameter(Mandatory=$true)]
        [string]$Key,

        [parameter(Mandatory=$true)]
        [string]$Name,
 
        [parameter(ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
        [string]$Computername="$env:COMPUTERNAME",

        [Parameter(Mandatory=$false)]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty
    )

    Begin
    {
        $reg = Get-WmiObject -List "StdRegprov" -ComputerName $computername -Credential $Credential
    }
    Process
    {
        # We get the correct value associated with the key.
        switch ($hive) 
        {
            "HKCR" {$reg_hive = 2147483648}
            "HKCU" {$reg_hive = 2147483649}
            "HKLM" {$reg_hive = 2147483650}
            "HKUS" {$reg_hive = 2147483651}
            "HKCC" {$reg_hive = 2147483653}
        }

        $valdata = $reg.EnumValues($reg_hive, $key)
        if ($valdata.returnvalue -eq 0) 
        {
            # check that the value actualy exists
            if ($valdata.snames -contains $Name) 
            {
                $data = $reg.DeleteValue($reg_hive, $Key, $Name)
                switch ($data.returnvalue)
                {
                    "0"       {Write-Verbose "Value $name has been removed."}
                    default {Write-Error "Error while removing value $name $($data.retunvalue)"}
                }
            }
            else
            {
                Write-Error "Value $name does not exist in key specified."
            }
        }
        elseif ($valdata.returnvalue -eq 2)
        {
            Write-Error "Key $key does not exist"
        }
        else
        {
            Write-Error "Error when removing value on key: $($data.ReturnValue)"
        }
    }
    End
    {
    }
}


<#
.Synopsis
   Gets the Security DACL and Owner of a given Registry Key.
.DESCRIPTION
   Gets the Security DACL and Owner of a given Registry Key.
.EXAMPLE
   Get the DACL for the SAM key in HKLM
   PS C:\> Get-RegKeySecurityDescriptor -Hive HKlm -Key sam

    Trustee Permission
    ------- ----------
    BUILTIN\Administrators Owner
    BUILTIN\Users Read Access
    BUILTIN\Administrators All Access
    NT AUTHORITY\SYSTEM All Access
    \CREATOR OWNER All Access
    APPLICATION PACKAGE AUTHORITY\ALL APPLICATION PACKAGES Read Access

#>

function Get-RegKeySecurityDescriptor
{
    [CmdletBinding()]
    Param
    (
        [parameter(Mandatory=$true)]
        [string]
        [Validateset(“HKCR”, “HKCU”, “HKLM”, "HKUS", "HKCC")]
        $Hive,

        [parameter(Mandatory=$true)]
        [string]$Key,
 
        [parameter(ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
        [string]$Computername="$env:COMPUTERNAME",

        [Parameter(Mandatory=$false)]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty
    )

    Begin
    {
        $reg = Get-WmiObject -List "StdRegprov" -ComputerName $computername -Credential $Credential
        $accessmask = @{
            "1" = "Query Value"
            "2" = "Set Value."
            "4" = "Create Subkey"
            "8" = "Enumerate Subkeys"
            "16" = "Notify"
            "32" = "Create Key"
            "65536" = "Delete Key"
            "131072" = "Read Control"
            "262144" = "Write DAC"
            "524288" = "Write Owner"
            "983103" = "All Access"
            "131097" = "Read Access"
        }
    }
    Process
    {
        # We get the correct value associated with the key.
        switch ($hive) 
        {
            "HKCR" {$reg_hive = 2147483648}
            "HKCU" {$reg_hive = 2147483649}
            "HKLM" {$reg_hive = 2147483650}
            "HKUS" {$reg_hive = 2147483651}
            "HKCC" {$reg_hive = 2147483653}
        }

        $data = $reg.GetSecurityDescriptor($reg_hive, $key)
        if ($data.ReturnValue -eq 0)
        {
            $owner = New-Object psobject
            Add-Member -InputObject $owner -MemberType NoteProperty -Name Trustee -Value "$($Data.Descriptor.Owner.Domain)\$($Data.Descriptor.Owner.Name)"
            Add-Member -InputObject $owner -MemberType NoteProperty -Name Permission -Value "Owner"
            $owner
            $data.Descriptor.DACL| ForEach-Object {
                $dacl = New-Object psobject
                Add-Member -InputObject $dacl -MemberType NoteProperty -Name Trustee -Value "$($_.Trustee.Domain)\$($_.Trustee.Name)"
                Write-Verbose "Access mask for $($_.Trustee.Domain)\$($_.Trustee.Name) is $($_.accessmask)"
                Add-Member -InputObject $dacl -MemberType NoteProperty -Name Permission -Value "$($accessmask[[string]$_.accessmask])"
                $dacl
            }
        }
        elseif ($data.ReturnValue -eq 2)
        {
            Write-Error "Key $key does not exist"
        }
        else
        {
            Write-Error "Error when creating key: $($data.ReturnValue)"
        }
    }
    End
    {
    }
}