Monday, June 11, 2012

Creating Registry Values

In a previous tip we introduced the new function New-RegKey that could create one or more registry keys. Here is a function that creates registry values. It uses New-RegKey internally if the key you specify does not yet exist, so make sure you include both functions
function New-Regkey {

$key = $key -replace ':',''
  $parts = $key -split '\\' 
  $tempkey = ''
  $parts | ForEach-Object {
$tempkey += ($_ + "\")
if ( (Test-Path "Registry::$tempkey"-eq $false)  {
New-Item "Registry::$tempkey"   | Out-Null
function Set-RegistryValue {
  $key = $key -replace ':',''
  New-Regkey $key
  $regkey = "Registry::$key"
  Set-ItemProperty -Path $regkey -Name $name -Value $value -Type $type
Now check out how easy it is to set a new registry value! If the key is missing, it will also be created:
Set-RegistryValue HKCU:\Software\TestKey Testvalue -Value 123 –Type DWORD