0

I am trying to create a registry key "ABCProxy" for an application say "Pricer"

Then I want add parameters and values to the key Enable x3 and RetryAttempts x5.

Is this the correct way to do it?

# Define the path to the registry key
$RegistryPath = 'HKLM\SOFTWARE\WOW6432Node\OurC\Pricer\Application\ABCProxy'

# Create the registry key
New-Item -Path $RegistryPath -Force

# Set a new value within the recreated registry key

{New-ItemProperty `
  -Path         $RegistryPath `
  -Name         'Enable' `
  -Value        '3' `
  -PropertyType 'Reg_DWORD'
}
{New-ItemProperty `
  -Path         $RegistryPath `
  -Name         'RetryAttempts' `
  -Value        '5' `
  -PropertyType 'Reg_DWORD'
}

# Verify the change

{Get-ItemProperty -Path $RegistryPath}

Edit: Added the question.

9
  • Oh hi, I am just wondering if this script is correct or not Commented Dec 6, 2024 at 9:01
  • 3
    $registryPath should be 'HKLM:\SOFTWARE\WOW6432Node\OurC\Pricer\Application\ABCProxy' or 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\OurC\Pricer\Application\ABCProxy' and why do you wrap the New-ItemProperty and Get-ItemProperty lines inside curly braces (to make them script blocks) ?? Commented Dec 6, 2024 at 9:57
  • 2
    Also, REG_DWORD means a 32-bit number, so you can omit the quotes around the values Commented Dec 6, 2024 at 10:02
  • 3
    hklm:\software not hklm\software, dword not reg_dword Commented Dec 6, 2024 at 13:26
  • Using backticks as line continuation character is considered bad style and should be avoided. If you want to make long code lines shorter you should consider using splatting! ☝🏼😉 Commented Jun 25 at 15:55

1 Answer 1

0

You never provided any errors about your attempt, which would have provided you with relevant information about your issue that would have helped you (and us) to solve the issue.

But in short; No, your code wont work. Your are on the right track though. If you read all the comments you would end with a code something like this...

# Define the path to the registry key
$RegistryPath = 'HKCU:\SOFTWARE\OurCompany\PricerApp\ABCProxy'

# Create the registry key
New-Item -Path $RegistryPath -Force

# Set a new value within the recreated registry key
$NewKey1 = @{
    Path            = $RegistryPath
    Name            = 'Enable'
    Value           = 3
    PropertyType    = 'DWORD'
}
New-ItemProperty @NewKey1

$NewKey2 = @{
    Path            = $RegistryPath
    Name            = 'RetryAttempts'
    Value           = 5
    PropertyType    = 'DWORD'
}
New-ItemProperty @NewKey2

# Verify the change
Get-ItemProperty -Path $RegistryPath

I changed the example path to be able to test it without being a local admin.

You can of course enter the values not using the technique of splatting (providing the parameters as a hash table). Splatting increases code readability but also, to some degree, the complexity.

New-ItemProperty -Path <value> -Name <value> -Value <value> -PropertyType <type>

Most often for pedagogic reasons, I choose to provide my answer using line breaks to avoid having the reader to scroll sideways...

New-ItemProperty `
  -Path  <value> `
  -Name  <value> `
  -Value <value> `
  -PropertyType <type>

But using the backtick is bad practice as it has low visibility and will break the code if you forgot about it when altering the amount of parameters or changes the order. Then splatting is a better choice.

One drawback with splatting though, is that empty values will be provided as $null parameters and will break some functions that doesn't properly handle $null values.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.