-
Notifications
You must be signed in to change notification settings - Fork 7
/
RegistryLibrary.psm1
526 lines (505 loc) · 15.4 KB
/
RegistryLibrary.psm1
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
Function Get-RegistryValue {
<#
.SYNOPSIS
This function method enumerates the values of the given subkey or returns the data value for a named value
.DESCRIPTION
Registry subkeys group entries with related information, and it is often useful to display that related
information. Unfortunately, this is not necessarily a straightforward procedure; after all, you cannot read
a registry value unless you use the appropriate method. But how can you call the appropriate method if you
do not know the data type of the value being read?
The majority of registry values that hold useful information for a system administrator are made up of either
alphanumeric characters (REG_SZ) or numbers (REG_DWORD). String values in the registry are often clearly
interpretable words, such as the name of a component manufacturer.
This function allows you to return a list of values in a given key and then using that information you can
easily use this function to get the specific data you are after.
.PARAMETER Hive
A registry tree, also known as a hive, that contains the SubKeyName path
.PARAMETER SubKeyName
A path that contains the named values to be enumerated
.PARAMETER ValueName
A named value whose data value you are retrieving. Specify an empty string to get the default named value
.PARAMETER ValueType
This is the type of data to be returned
.PARAMETER ComputerName
The name of a remote computer to query, the default is localhost
.EXAMPLE
Get-RegistryValue -Hive HKEY_LOCAL_MACHINE -SubKeyName "SOFTWARE\Microsoft\Internet Explorer" -ValueName "Version" -ValueType String
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 2
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
sValue : 9.11.9600.17498
PSComputerName :
Description
-----------
This example shows getting the version value from the internet explorer registry
key.
.EXAMPLE
Get-RegistryValue -Hive HKEY_LOCAL_MACHINE -SubKeyName "SOFTWARE\Microsoft\Internet Explorer"
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 3
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
sNames : {Version, svcKBFWLink, svcVersion, svcUpdateVersion...}
Types : {1, 1, 1, 1...}
PSComputerName :
Description
-----------
This example shows how to list all the values in a given key, simply omit the ValueName param.
.NOTES
FunctionName : Get-RegistryValue
Created by : jspatton
Date Coded : 02/03/2015 09:48:47
.LINK
https://github.com/jeffpatton1971/mod-posh/wiki/RegistryLibrary#Get-RegistryValue
.LINK
https://msdn.microsoft.com/en-us/library/aa390440(v=vs.85).aspx
.LINK
https://msdn.microsoft.com/en-us/library/aa390445(v=vs.85).aspx
.LINK
https://msdn.microsoft.com/en-us/library/aa390455(v=vs.85).aspx
.LINK
https://msdn.microsoft.com/en-us/library/aa390458(v=vs.85).aspx
.LINK
https://msdn.microsoft.com/en-us/library/aa390788(v=vs.85).aspx
.LINK
https://msdn.microsoft.com/en-us/library/aa390388(v=vs.85).aspx
#>
[CmdletBinding()]
Param
(
[ValidateSet("HKEY_CLASSES_ROOT", "HKEY_CURRENT_USER", "HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CURRENT_CONFIG", "HKEY_DYN_DATA")]
[string]$Hive,
[string]$SubKeyName,
[string]$ValueName,
[ValidateSet("DWORD", "QWORD", "String", "MultiString", "ExpandedString", "Binary")]
[string]$ValueType,
[string]$ComputerName = "."
)
Begin {
switch ($Hive) {
"HKEY_CLASSES_ROOT" {
[uint32]$hDefKey = 2147483648;
}
"HKEY_CURRENT_USER" {
[uint32]$hDefKey = 2147483649;
}
"HKEY_LOCAL_MACHINE" {
[uint32]$hDefKey = 2147483650;
}
"HKEY_USERS" {
[uint32]$hDefKey = 2147483651;
}
"HKEY_CURRENT_CONFIG" {
[uint32]$hDefKey = 2147483653;
}
"HKEY_DYN_DATA" {
[uint32]$hDefKey = 2147483654;
}
}
[string]$sSubKeyName = $SubKeyName;
[string]$sValueName = $ValueName;
}
Process {
$StandardRegistryProvider = [wmiclass]"\\$($ComputerName)\root\cimv2:StdRegProv";
switch ($ValueType) {
"DWORD" {
$rValue = $StandardRegistryProvider.GetDWORDValue($hDefKey, $sSubKeyName, $sValueName);
}
"QWORD" {
$rValue = $StandardRegistryProvider.GetQWORDValue($hDefKey, $sSubKeyName, $sValueName);
}
"String" {
$rValue = $StandardRegistryProvider.GetStringValue($hDefKey, $sSubKeyName, $sValueName);
}
"MultiString" {
$rValue = $StandardRegistryProvider.GetMultiStringValue($hDefKey, $sSubKeyName, $sValueName);
}
"ExpandedString" {
$rValue = $StandardRegistryProvider.GetExpandedStringValue($hDefKey, $sSubKeyName, $sValueName);
}
"Binary" {
$rValue = $StandardRegistryProvider.GetBinaryValue($hDefKey, $sSubKeyName, $sValueName);
}
default {
$rValue = $StandardRegistryProvider.EnumValues($hDefKey, $sSubKeyName);
}
}
}
End {
return $rValue;
}
}
Function Set-RegistryValue {
<#
.SYNOPSIS
This function sets the data value for a named value
.DESCRIPTION
Changing the values of registry entries is a common registry management task. The most challenging
aspect of that task is determining which entries you need to change, and to which values they must
be changed. After you have this information, you must choose the appropriate Registry Provider
methods for making the change.
This function allows you to do this by simply choosing the appropriate ValueType on the command line
.PARAMETER Hive
A registry tree, also known as a hive, that contains the SubKeyName path
.PARAMETER SubKeyName
A path that contains the named values to be enumerated
.PARAMETER ValueName
A named value whose data value you are retrieving. Specify an empty string to get the default named value
.PARAMETER ValueType
This is the type of data to be returned
.PARAMETER Value
The value to be stored in the key
.PARAMETER ComputerName
The name of a remote computer to query, the default is localhost
.EXAMPLE
Set-RegistryValue -Hive HKEY_LOCAL_MACHINE -SubKeyName "SOFTWARE\Test" -ValueName "testing" -ValueType String -Value "tval"
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
PSComputerName :
.NOTES
FunctionName : Set-RegistryValue
Created by : jspatton
Date Coded : 02/03/2015 10:50:52
.LINK
https://github.com/jeffpatton1971/mod-posh/wiki/RegistryLibrary#Set-RegistryValue
.LINK
https://msdn.microsoft.com/en-us/library/aa393286(v=vs.85).aspx
.LINK
https://msdn.microsoft.com/en-us/library/aa393297(v=vs.85).aspx
.LINK
https://msdn.microsoft.com/en-us/library/aa393590(v=vs.85).aspx
.LINK
https://msdn.microsoft.com/en-us/library/aa393299(v=vs.85).aspx
.LINK
https://msdn.microsoft.com/en-us/library/aa393465(v=vs.85).aspx
.LINK
https://msdn.microsoft.com/en-us/library/aa393600(v=vs.85).aspx
#>
[CmdletBinding()]
Param
(
[ValidateSet("HKEY_CLASSES_ROOT", "HKEY_CURRENT_USER", "HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CURRENT_CONFIG", "HKEY_DYN_DATA")]
[string]$Hive,
[string]$SubKeyName,
[string]$ValueName,
[ValidateSet("DWORD", "QWORD", "String", "MultiString", "ExpandedString", "Binary")]
[string]$ValueType,
$Value,
[string]$ComputerName = "."
)
Begin {
switch ($Hive) {
"HKEY_CLASSES_ROOT" {
[uint32]$hDefKey = 2147483648;
}
"HKEY_CURRENT_USER" {
[uint32]$hDefKey = 2147483649;
}
"HKEY_LOCAL_MACHINE" {
[uint32]$hDefKey = 2147483650;
}
"HKEY_USERS" {
[uint32]$hDefKey = 2147483651;
}
"HKEY_CURRENT_CONFIG" {
[uint32]$hDefKey = 2147483653;
}
"HKEY_DYN_DATA" {
[uint32]$hDefKey = 2147483654;
}
}
[string]$sSubKeyName = $SubKeyName;
[string]$sValueName = $ValueName;
}
Process {
$StandardRegistryProvider = [wmiclass]"\\$($ComputerName)\root\cimv2:StdRegProv";
switch ($ValueType) {
"DWORD" {
[uint32]$uValue = $Value;
$rValue = $StandardRegistryProvider.SetDWORDValue($hDefKey, $sSubKeyName, $sValueName, $uValue);
}
"QWORD" {
[uint64]$uValue = $Value;
$rValue = $StandardRegistryProvider.SetQWORDValue($hDefKey, $sSubKeyName, $sValueName, $uValue);
}
"String" {
[string]$sValue = $Value;
$rValue = $StandardRegistryProvider.SetStringValue($hDefKey, $sSubKeyName, $sValueName, $sValue);
}
"MultiString" {
[string[]]$sValue = $Value;
$rValue = $StandardRegistryProvider.SetMultiStringValue($hDefKey, $sSubKeyName, $sValueName, $sValue);
}
"ExpandedString" {
[string]$sValue = $Value;
$rValue = $StandardRegistryProvider.SetExpandedStringValue($hDefKey, $sSubKeyName, $sValueName, $sValue);
}
"Binary" {
[uint8array]$uValue = $Value;
$rValue = $StandardRegistryProvider.SetBinaryValue($hDefKey, $sSubKeyName, $sValueName, $uValue);
}
}
}
End {
return $rValue;
}
}
Function New-RegistryKey {
<#
.SYNOPSIS
This function creates a subkey in the specified tree
.DESCRIPTION
You rarely need to create a registry subkey. However, in some circumstances
the solution to a problem - as described in a Knowledge Base article or a
security bulletin - requires the addition of a subkey. If you need to create
a subkey on a large number of computers, the best solution might very well be
a script. After all, a single script can be used to create the subkey on every
computer affected by the problem.
.PARAMETER Hive
A registry tree, also known as a hive, that contains the SubKeyName path
.PARAMETER SubKeyName
A path that contains the named values to be enumerated
.PARAMETER ComputerName
The name of a remote computer to query, the default is localhost
.EXAMPLE
New-RegistryKey -Hive HKEY_LOCAL_MACHINE -SubKeyName "SOFTWARE\Test"
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
PSComputerName :
.NOTES
FunctionName : New-RegistryKey
Created by : jspatton
Date Coded : 02/03/2015 11:00:40
.LINK
https://github.com/jeffpatton1971/mod-posh/wiki/RegistryLibrary#New-RegistryKey
.LINK
https://msdn.microsoft.com/en-us/library/aa389385(v=vs.85).aspx
#>
[CmdletBinding()]
Param
(
[ValidateSet("HKEY_CLASSES_ROOT", "HKEY_CURRENT_USER", "HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CURRENT_CONFIG", "HKEY_DYN_DATA")]
[string]$Hive,
[string]$SubKeyName,
[string]$ComputerName = "."
)
Begin {
switch ($Hive) {
"HKEY_CLASSES_ROOT" {
[uint32]$hDefKey = 2147483648;
}
"HKEY_CURRENT_USER" {
[uint32]$hDefKey = 2147483649;
}
"HKEY_LOCAL_MACHINE" {
[uint32]$hDefKey = 2147483650;
}
"HKEY_USERS" {
[uint32]$hDefKey = 2147483651;
}
"HKEY_CURRENT_CONFIG" {
[uint32]$hDefKey = 2147483653;
}
"HKEY_DYN_DATA" {
[uint32]$hDefKey = 2147483654;
}
}
[string]$sSubKeyName = $SubKeyName;
}
Process {
$StandardRegistryProvider = [wmiclass]"\\$($ComputerName)\root\cimv2:StdRegProv";
$rValue = $StandardRegistryProvider.CreateKey($hDefKey, $sSubKeyName);
}
End {
return $rValue;
}
}
Function Remove-RegistryKey {
<#
.SYNOPSIS
This function deletes a subkey in the specified tree
.DESCRIPTION
This function deletes a subkey in the specified tree
.PARAMETER Hive
A registry tree, also known as a hive, that contains the SubKeyName path
.PARAMETER SubKeyName
A path that contains the named values to be enumerated
.PARAMETER ComputerName
The name of a remote computer to query, the default is localhost
.EXAMPLE
Remove-RegistryKey -Hive HKEY_LOCAL_MACHINE -SubKeyName "SOFTWARE\Test"
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
PSComputerName :
.NOTES
FunctionName : Remove-RegistryKey
Created by : jspatton
Date Coded : 02/03/2015 11:41:58
.LINK
https://github.com/jeffpatton1971/mod-posh/wiki/RegistryLibrary#Remove-RegistryKey
.LINK
https://msdn.microsoft.com/en-us/library/aa389869(v=vs.85).aspx
#>
[CmdletBinding()]
Param
(
[ValidateSet("HKEY_CLASSES_ROOT", "HKEY_CURRENT_USER", "HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CURRENT_CONFIG", "HKEY_DYN_DATA")]
[string]$Hive,
[string]$SubKeyName,
[string]$ComputerName = "."
)
Begin {
switch ($Hive) {
"HKEY_CLASSES_ROOT" {
[uint32]$hDefKey = 2147483648;
}
"HKEY_CURRENT_USER" {
[uint32]$hDefKey = 2147483649;
}
"HKEY_LOCAL_MACHINE" {
[uint32]$hDefKey = 2147483650;
}
"HKEY_USERS" {
[uint32]$hDefKey = 2147483651;
}
"HKEY_CURRENT_CONFIG" {
[uint32]$hDefKey = 2147483653;
}
"HKEY_DYN_DATA" {
[uint32]$hDefKey = 2147483654;
}
}
[string]$sSubKeyName = $SubKeyName;
}
Process {
$StandardRegistryProvider = [wmiclass]"\\$($ComputerName)\root\cimv2:StdRegProv";
$rValue = $StandardRegistryProvider.DeleteKey($hDefKey, $sSubKeyName);
}
End {
return $rValue;
}
}
Function Get-RegistryKey {
<#
.SYNOPSIS
This function enumerates the subkeys for a path
.DESCRIPTION
In some instances, useful configuration information is stored as the names
of a set of subkeys. In a case such as this, simply listing the names of
the subkeys provides useful information.
This function enables you to return the subkeys of a registry key or subkey.
Note that the function returns only the immediate subkeys of a key or subkey;
it does not return any subkeys that might be contained within those top-level
subkeys.
.PARAMETER Hive
A registry tree, also known as a hive, that contains the SubKeyName path
.PARAMETER SubKeyName
A path that contains the named values to be enumerated
.PARAMETER ComputerName
The name of a remote computer to query, the default is localhost
.EXAMPLE
Get-RegistryKey -Hive HKEY_LOCAL_MACHINE -SubKeyName "SOFTWARE"
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 2
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0
sNames : {Classes, Clients, IM Providers, Intel...}
PSComputerName :
.NOTES
FunctionName : Get-RegistryKey
Created by : jspatton
Date Coded : 02/03/2015 11:52:49
.LINK
https://github.com/jeffpatton1971/mod-posh/wiki/RegistryLibrary#Get-RegistryKey
.LINK
https://msdn.microsoft.com/en-us/library/aa390387(v=vs.85).aspx
#>
[CmdletBinding()]
Param
(
[ValidateSet("HKEY_CLASSES_ROOT", "HKEY_CURRENT_USER", "HKEY_LOCAL_MACHINE", "HKEY_USERS", "HKEY_CURRENT_CONFIG", "HKEY_DYN_DATA")]
[string]$Hive,
[string]$SubKeyName,
[string]$ComputerName = "."
)
Begin {
switch ($Hive) {
"HKEY_CLASSES_ROOT" {
[uint32]$hDefKey = 2147483648;
}
"HKEY_CURRENT_USER" {
[uint32]$hDefKey = 2147483649;
}
"HKEY_LOCAL_MACHINE" {
[uint32]$hDefKey = 2147483650;
}
"HKEY_USERS" {
[uint32]$hDefKey = 2147483651;
}
"HKEY_CURRENT_CONFIG" {
[uint32]$hDefKey = 2147483653;
}
"HKEY_DYN_DATA" {
[uint32]$hDefKey = 2147483654;
}
}
[string]$sSubKeyName = $SubKeyName;
}
Process {
$StandardRegistryProvider = [wmiclass]"\\$($ComputerName)\root\cimv2:StdRegProv";
$rValue = $StandardRegistryProvider.EnumKey($hDefKey, $sSubKeyName);
}
End {
return $rValue;
}
}
Export-ModuleMember *