Use PowerShell to Create Registry Keys

Use PowerShell to Create Registry Keys

  • Comments 5
  • Likes

Summary: Use Windows PowerShell to create and manage registry keys.

Microsoft Scripting Guy, Ed Wilson, is here. Today I am happy to provide you with an excerpt from my book, Windows PowerShell 3.0 Step by Step, published by Microsoft Press.

Image of book cover

Note  The registry contains information that is vital to the operation and configuration of your computer. Serious problems could arise if you edit the registry incorrectly. Therefore, it is important to back up your system prior to attempting to make any changes. For information about backing up your registry, see article 322756 in the Microsoft Knowledge Base. For general information about working with the registry, see article 310516.

Creating a new registry key by using Windows PowerShell is the same as creating a new file or a new folder. All three processes use the New-Item cmdlet. In addition, you might use the Test-Path cmdlet to determine if the registry key already exists. You may also want to change your working location to one of the registry drives. If you do this, you might use the Push-Location, Set-Location, and Pop-Location cmdlets. This is, of course, the long way of doing things…

Just the steps

  1. Store the current working location by using the Push-Location cmdlet.
  2. Change the current working location to the appropriate registry drive by using the Set-Location cmdlet.
  3. Use the Test-Path cmdlet to determine if the registry key already exists.
  4. Use the New-Item cmdlet to create the new registry key.
  5. Use the Pop-Location cmdlet to return to the starting working location.

The following example creates a new registry key named hsg off the HKEY_CURRENT_USERS software registry hive. It illustrates each of the five steps previously detailed.

Push-Location
Set-Location HKCU:
Test-Path .\Software\test
New-Item -Path .\Software -Name test
Pop-Location

The commands and the associated output from the commands are shown in the following image:

Image of command output

The short way to create a new registry key

It is not always necessary to change the working location to a registry drive when you create a new registry key. In fact, it is not even necessary to use the Test-Path cmdlet to determine if the registry key exists. If the registry key already exists, an error generates. If you want to overwrite the registry key, use the Force parameter.

Note  How you choose to deal with an already existing registry key is one of those design decisions that confront IT pros who venture very far into the world of scripting. Software developers are very familiar with these types of decisions, and they usually deal with them during the analyzing requirements portion of the development lifecycle. IT pros who open the Windows PowerShell ISE first, and think about the design requirements second, become easily stymied, or else write-in problems. For more information about this, see my Microsoft Press book, Windows PowerShell 2.0 Best Practices.

The following example creates a new registry key named test in the HKCU:\Software location. Because the command includes the full path, it does not need to execute from the HKCU drive. Because the command uses the Force switched parameter, the command overwrites the HKCU:\Software\test registry key if it already exists.

New-Item -Path HKCU:\Software -Name test –Force

Just the steps: The short way to create a new registry key

  1. Include the full path to the registry key to create.
  2. Use the Force parameter to overwrite any existing registry key of the same name.

In the following image, the first attempt to create a test registry key fails because it already exists. The second command uses the Force parameter, which causes the command to overwrite the existing registry key, and therefore it succeeds without creating an error.

Image of command output

Setting the default value for the key

The previous examples do not set the default value for the newly created registry key. If the registry key already exists (as it does in this specific case), use the Set-Item cmdlet to assign a default value to the registry key. The steps to accomplish this are shown here:

Just the steps: Assign a default value to an existing registry key

  1. Use the Set-Item cmdlet and supply the complete path to the existing registry key.
  2. Supply the default value in the Value parameter of the Set-Item cmdlet.

The following command assigns the value “test key” to the default property value of the hsg registry key contained in the HKCU:\Software location.

Set-Item -Path HKCU:\Software\test -Value "test key"

Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • You need to be careful with using -force to overwrite an existing registry key, because it will delete any keys or values that exist under the key.

  • @disaak you are correct. Thank you for bringing this up. As with all things, one should always backup the registry before making any changes. Here is a good link: blogs.technet.com/.../use-powershell-to-back-up-system-state-prior-to-making-changes.aspx

  • How do you search the registry to find a 'known' key in an 'unknown' place?