Last active
November 12, 2017 17:39
-
-
Save techthoughts2/cc2ffc159e422a8faf80f48e3d486815 to your computer and use it in GitHub Desktop.
Will create a string value in registry key specified
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Will create a string value in registry key specified | |
.DESCRIPTION | |
Evaluates provided registry path and determines if key is present. Key will be created if not found. Once verified the string value specified will be created in the registry location. | |
.PARAMETER RegKeyPath | |
Registry key path - must be in PS shorthand. Ex: HKLM:\SECURITY | |
.PARAMETER Name | |
The value name to be created. Equivalent to REG_SZ | |
.PARAMETER Value | |
The string value that will be loaded into the item | |
.EXAMPLE | |
Set-Registrykey -RegKeyPath HKLM:\SOFTWARE\HyperV\Viridian -Name Viridian -Value History | |
Verifies if the specified registry key exists. It will be created if not found. Once verified the Viridian String will be created and the value History will be loaded into it. Only a true/false value will be returned based on success. | |
.EXAMPLE | |
Set-Registrykey -RegKeyPath HKLM:\SOFTWARE\HyperV\Viridian -Name Viridian -Value History -Verbose | |
Verifies if the specified registry key exists. It will be created if not found. Once verified the Viridian String will be created and the value History will be loaded into it. Verbose output will be displayed. | |
.OUTPUTS | |
Boolean value | |
.NOTES | |
Author: Jake Morrison | |
http://techthoughts.info | |
PS Shorthand reg keys must be used | |
ACCEPTABLE: HKLM:\SECURITY | |
NOT ACCEPTABLE: HKEY_LOCAL_MACHINE\SECURITY | |
#> | |
function Set-Registrykey { | |
[CmdletBinding(SupportsShouldProcess = $true)] | |
Param | |
( | |
[Parameter(Mandatory = $true, | |
Position = 0, | |
HelpMessage = 'Registry key path')] | |
[ValidateNotNull()] | |
[ValidateNotNullOrEmpty()] | |
[String] | |
$RegKeyPath, | |
[Parameter(Mandatory = $true, | |
Position = 1, | |
HelpMessage = 'Value Name')] | |
[ValidateNotNull()] | |
[ValidateNotNullOrEmpty()] | |
[String] | |
$Name, | |
[Parameter(Mandatory = $true, | |
Position = 2, | |
HelpMessage = 'Value data')] | |
[ValidateNotNull()] | |
[ValidateNotNullOrEmpty()] | |
[String] | |
$Value | |
) | |
$successEval = $true #assume the best | |
try { | |
#if the path doesn't exist, we will create it. | |
Write-Verbose -Message "Evaluating if $RegKeyPath is present." | |
if (!(Test-Path -Path $RegKeyPath -ErrorAction Stop)) { | |
Write-Verbose -Message "Reg Key not found. Creating Reg Key..." | |
New-Item -Path $RegKeyPath –Force -ErrorAction Stop | Out-Null | |
Write-Verbose -Message "Reg Key created successfully." | |
} | |
else { | |
Write-Verbose -Message "Reg key verified present." | |
} | |
Write-Verbose -Message "Creating $Name with value: $value" | |
New-ItemProperty -Path $RegKeyPath -Name $Name -Value $Value ` | |
-PropertyType String -Force -ErrorAction Stop | Out-Null | |
Write-Verbose -Message "Success." | |
} | |
catch { | |
Write-Verbose -Message "An error was ecountered:" | |
Write-Error $_ | |
$successEval = $false | |
} | |
return $successEval | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment