-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistry.psm1
More file actions
55 lines (48 loc) · 1.73 KB
/
Registry.psm1
File metadata and controls
55 lines (48 loc) · 1.73 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
<#
Registry edit functions.
Gabriel Polmar, Megaphat Networks
https://www.megaphat.net
gpolmar@megaphat.net
By using these functions you AGREE to assign credit to the author listed above.
This is a Windows PowerShell Module.
Use:
First Import the module into your code by using:
Import-Module Registry.psm1
To set a registry value, use as follows:
regSet <Path> <Item> <Value>
Example: regSet "HKLM:\Software\MegaphatNetworks\Powershell\TestKey" "TestDWord" 1
Example: regSet "HKLM:\Software\MegaphatNetworks\Powershell\TestKey" "TestString" "https://www.megaphat.net"
Path always starts with the top-level Key such as:
HKLM: HKEY_LOCAL_MACHINE
HKCU: HKEY_CURRENT_USER
HKCR: HKEY_CLASS_ROOT
If a path does not exist in the top-level key, the function will parse the string of the path and create the path and all levels.
This function does not return a value nor does it display a response on the console.
To get a registry value, use as follows:
If (regGet <Path> <Item>) {#Registry Entry Exists}
If (!(regGet <Path> <Item>)) {#Registry Entry Does Not Exist}
$ThisVal = regGet <Path> <Item>
$ThisVal = regGet "HKLM:\Software\MegaphatNetworks\Powershell\TestKey" "TestString"
Write-Host "Value: $ThisVal"
#>
Function regSet ($KeyPath, $KeyItem, $KeyValue) {
$Key = $KeyPath.Split("\")
ForEach ($Level in $Key) {
If (!($ThisKey)) {
$ThisKey = "$Level"
} Else {
$ThisKey = "$ThisKey\$Level"
}
If (!(Test-Path $ThisKey)) {New-Item $ThisKey -Force | out-null}
}
Set-ItemProperty $KeyPath $KeyItem -Value $KeyValue
}
Function regGet($Key, $Item) {
If (!(Test-Path $Key)) {
Return
} Else {
If (!($Item)) {$Item = "(Default)"}
$ret = (Get-ItemProperty -Path $Key -Name $Item).$Item
Return $ret
}
}