-
Notifications
You must be signed in to change notification settings - Fork 11
/
Tips - Create registry values.ps1
56 lines (47 loc) · 2.08 KB
/
Tips - Create registry values.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
# RUN AS ADMINISTRATOR if you modify HKLM due to security restrictions
# New key : complete Path
New-Item -Path 'HKCU:\SOFTWARE\Test'
# New Key: Path + Name
New-Item -Path 'HKCU:\SOFTWARE\Test' -Name 'App'
Get-ChildItem -Path 'HKCU:\SOFTWARE\Test'
# Replace Existing Key
New-Item -Path 'HKCU:\SOFTWARE\Test' -Force
Get-ChildItem -Path 'HKCU:\SOFTWARE\Test'
<# With the parameter -Force, it replace the existing key
Note that All Items in this key disapear
#>
# Create and assign a Value
New-Item -Path 'HKCU:\SOFTWARE\Test\Website' -Value 'Https://www.mywebsite.com'
# Note that the default key name is "Default"
Get-Item -Path 'HKCU:\SOFTWARE\Test\Website'
Get-ItemProperty -Path 'HKCU:\SOFTWARE\Test\Website'
# Rename a value
Rename-ItemProperty -Path 'HKCU:\SOFTWARE\Test\Website' -Name "(default)" -NewName "WebSite"
Get-ItemProperty -Path 'HKCU:\SOFTWARE\Test\Website'
# Note that the "(default)" key still exists but the value is set to $null
# Add another key/value
New-ItemProperty -Path 'HKCU:\SOFTWARE\Test\Website' -Name "website2" -Value 'Https://www.mywebsite2.com'
# Define the PropertyType of a value
$Name = "Version"
$Value = "1.0"
$PropertyType = "DWORD"
New-ItemProperty -Path 'HKCU:\SOFTWARE\Test\Website' -Name $Name -Value $Value -PropertyType $PropertyType
# Testing the existence of the registry Key and the value
$RegistryPath = 'HKCU:\SOFTWARE\Test\Website'
$Name = "Version"
$Value = "2"
if (Test-Path $RegistryPath)
{
New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType DWORD -Force
Write-host "The Key is existing... and the value updated" -ForegroundColor Green
}
else { # registry Key doesn't exist
New-Item -Path $registryPath -Force | Out-Null # to avoid console output
New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType DWORD -Force | Out-Null
}
Get-ItemProperty -Path $RegistryPath -Name $Name
# Tips to save time
Set-Location -Path 'HKCU:\SOFTWARE\Test\'
# and now use the cmdlet Pop-Location to return to the starting working location.
Pop-Location
New-Item -Path .\Website\ -Name 'AnotherKey'