-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set-RegKey.ps1
87 lines (81 loc) · 3.96 KB
/
Set-RegKey.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
<#
.SYNOPSIS
Set registry key to your desired value & return action taken
(w/ silent option)
.NOTES
Name: Set-RegKey
Author: Payton Flint
Version: 1.2
DateCreated: 2023-Aug
.LINK
https://paytonflint.com/powershell-add-modify-registry-key/
https://github.com/p8nflnt/SysAdmin-Toolbox/blob/main/Set-RegKey.ps1
#>
$regPath = '<INSERT PATH>'
$regName = '<INSERT NAME>'
$regValue = '<INSERT VALUE>'
$propType = 'DWord' # 'String', 'ExpandString', 'Binary', 'DWord', 'MultiString', 'QWord', 'Unknown'
Function Set-RegKey {
param(
$regPath,
$regName,
$regValue,
$propType,
[bool]$silent
)
$regFull = Join-Path $regPath $regName
Try {
$CurrentKeyValue = (Get-ItemProperty -Path $regPath -Name $regName -ErrorAction SilentlyContinue).$regName
If (Test-Path $regPath) {
If ($CurrentKeyValue -eq $regValue) {
If (!($silent)) {
Write-Host -ForegroundColor Green 'Registry key' $regFull 'value is set to the desired value of' $regValue'.'
}
$script:regTest = $True
} Else {
If (!($silent)) {
Write-Host -ForegroundColor Red 'Registry key' $regFull 'value is not' $regValue'.'
Write-Host -ForegroundColor Cyan 'Setting registry key' $regFull 'value to' $regValue'.'
}
New-ItemProperty -Path $regPath -Name $regName -Value $regValue -PropertyType $propType -Force | Out-Null
$CurrentKeyValue = (Get-ItemProperty -Path $regPath -Name $regName -ErrorAction SilentlyContinue).$regName
If ($CurrentKeyValue -eq $regValue) {
If (!($silent)) {
Write-Host -ForegroundColor Green 'Registry key' $regFull 'value is set to the desired value of' $regValue'.'
}
$script:regTest = $True
} Else {
If (!($silent)) {
Write-Host -ForegroundColor Red 'Registry key' $regFull 'value could not be set to' $regValue '.'
}
}
}
} Else {
If (!($silent)) {
Write-Host -ForegroundColor Red 'Registry key' $regFull 'path does not exist.'
Write-Host -ForegroundColor Cyan 'Creating registry key' $regFull'.'
}
New-Item -Path $regPath -Force | Out-Null
If (!($silent)) {
Write-Host -ForegroundColor Cyan 'Setting registry key' $regFull 'value to' $regValue'.'
}
New-ItemProperty -Path $regPath -Name $regName -Value $regValue -PropertyType $propType -Force | Out-Null
$CurrentKeyValue = (Get-ItemProperty -Path $regPath -Name $regName -ErrorAction SilentlyContinue).$regName
If ($CurrentKeyValue -eq $regValue) {
If (!($silent)) {
Write-Host -ForegroundColor Green 'Registry key' $regFull 'value is set to the desired value of' $regValue'.'
}
$script:regTest = $True
} Else {
If (!($silent)) {
Write-Host -ForegroundColor Red 'Registry key' $regFull 'value could not be set to' $regValue '.'
}
}
}
} Catch {
If (!($silent)) {
Write-Host -ForegroundColor Red 'Registry key' $regFull 'value could not be set to' $regValue '.'
}
}
} # End Function Set-RegKey
Set-RegKey -regPath $regPath -regName $regName -regValue $regValue -propType $propType -silent <BOOL>