Sign in
|
Join PowerShell.com!
|
Help
Home
PowerTips
Ask the Experts
Forums
Webcasts
Blogs
eBookV2
Script Library
Twitter Grid
Modules
QuickClick
Scripts
Snippets
Videos
Library
»
Snippet Library
»
Registry
»
Slideshow
Registry Slideshow
Share
|
Browse Library
Module Library
QuickClick Library
Script Library
Snippet Library
ADSI
Comparison
Conditions and Loops
.Net
Exception Handlers
Files and Folders
Functions
Headers
Includes
Network
Pipeline
PowerShell Internals
Registry
Samples
Script Functions Templates
Security
SQL Server
Templates
User Management
WMI
Video Library
Members Only
Tags
Add Value
DWORD
Registry
Snippet
View more
Function to Update , Create and Read Registry in Local Machine
Function
RegUpdate {
param
(
[
string
]
$regKey
,
[
string
]
$Name
,
[
string
]
$data
,
[
string
]
$type
)
If
(
$data
-eq
"null"
)
{
$data
=
"null"
}
Write-Host
"Value Data is >>>> $data"
Write-host
"Create/ Update the Registry Value"
Write-Host
"Checking the machine to see $regKey"
$pattern
=
"(.\w+).+"
$reg
=
[
regex
]
$pattern
#Write-host $reg
$value
=
$regkey
-match
$reg
If
((
$matches
[1]
-eq
"HKLM"
)
-or
(
$matches
[1]
-eq
"HKEY_LOCAL_MACHINE"
)) {
$old
=
$matches
[1]
+
"\"
$updateKey
=
$regKey
.
replace
(
$old
,
"HKLM:\"
)
}
else
{
$pattern
=
"(.\w)+."
$reg
=
[
regex
]
$pattern
$value
=
$regkey
-match
$reg
If
((
$matches
[0]
-eq
"HKCU"
)
-or
(
$matches
[0]
-eq
"HKEY_CURRENT_USER"
)) {
$old
=
$matches
[0]
+
"\"
$updateKey
=
$regKey
.
replace
(
$old
,
"HKCU:\"
)
}
}
if
(
Test-Path
-Path
$updateKey
) {
Write-Host
"Registry Hive found $updatekey"
Write-Host
"Checking if the Value of the RegKey is already Present"
Write-Host
"Value is Present. Finding the Value Data for the $name"
$Valuedata
=
(
Get-ItemProperty
-Path
$updateKey
).
$Name
#Remove-ItemProperty -Path $updateKey -name $Name
if
(
$data
-eq
"null"
) {
Write-Host
"Reg Key:
$updateKey"
Write-Host
"Value Name:
$Name"
Write-Host
"Value data:
$Valuedata"
return
}
if
(
$Valuedata
-ne
$null
) {
if
(
$Valuedata
-eq
$data
) {
Write-Host
"====================================================================="
Write-Host
"Found the key. No Change made to the Registry"
Write-Host
"Reg Key:
$updateKey"
Write-Host
"Value Name:
$Name"
Write-Host
"Value data:
$Valuedata"
Write-Host
"Scripting Ending.... "
Write-Host
"====================================================================="
return
}
else
{
Write-Host
"Value Not found $updatekey"
Write-Host
"Current Registry value is
$Valuedata"
Write-Host
"====================================================================="
Write-Host
"Creating the Registry key"
Set-ItemProperty
-Path
$updateKey
-Name
$Name
-Value
$data
Write-Host
"====================================================================="
$Valuedata
=
(
Get-ItemProperty
-Path
$updateKey
).
$Name
if
(
$Valuedata
-eq
$data
) {
Write-Host
"====================================================================="
Write-Host
"Reg Key:
$updateKey"
Write-Host
"Value Name:
$Name"
Write-Host
"Value data:
$Valuedata"
Write-Host
"Found the key."
Write-Host
"Successfully Created the Registry Key"
Write-Host
"S U C C E S S"
Write-Host
"Scripting Ending.... "
Write-Host
"====================================================================="
return
}
}
}
else
{
Write-Host
"Value does not exist on the machine"
Write-Host
"====================================================================="
Write-Host
"Creating the Registry key"
New-ItemProperty
-Path
$updateKey
-Name
$Name
-Value
$data
-PropertyType
$type
|
Out-Null
Write-Host
"====================================================================="
Write-Host
"Checking if the Registry key is created on the machine"
$Valuedata
=
(
Get-ItemProperty
-Path
$updateKey
).
$Name
if
(
$Valuedata
-eq
$data
) {
Write-Host
"====================================================================="
Write-Host
"Reg Key:
$updateKey"
Write-Host
"Value Name:
$Name"
Write-Host
"Value data:
$Valuedata"
Write-Host
"Found the key."
Write-Host
"Successfully Created the Registry Key"
Write-Host
"S U C C E S S"
Write-Host
"Scripting Ending.... "
Write-Host
"====================================================================="
return
}
else
{
Write-Host
"====================================================================="
Write-Host
"Registry Key is not present."
Write-Host
"F A I L"
Write-Host
"Scripting Ending.... "
Write-Host
"====================================================================="
return
}
}
}
}
#usage
# Call the Function by passing the Registry Key , Value Name , Value Data , Type"
# This Function Create/Update the Exitng value
# Regupdate "HKEY_LOCAL_MACHINE\SOFTWARE\TEST" "E2" "0" "Dword"
# Regupdate "HKEY_LOCAL_MACHINE\SOFTWARE\TEST" "E2" "0" "Dword"
# Regupdate "HKEY_LOCAL_MACHINE\SOFTWARE\TEST" "Entry" "Newupdate"
# Pass "Null"
for the Third Argument while calling the Function if you want to retive the Registry Value Data"
# Regupdate "HKEY_LOCAL_MACHINE\SOFTWARE\TEST" "Entry" "Null"
Loading...
Function to...
Search Registry
Enumerate Subkeys
Enumerate Subkeys...
Create a new...
Add DWORD value
Add value to...
View all files
Copyright 2011 PowerShell.com. All rights reserved.