{"id":2446,"date":"2013-12-10T00:01:00","date_gmt":"2013-12-10T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/12\/10\/use-powershell-to-create-registry-keys\/"},"modified":"2013-12-10T00:01:00","modified_gmt":"2013-12-10T00:01:00","slug":"use-powershell-to-create-registry-keys","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-registry-keys\/","title":{"rendered":"Use PowerShell to Create Registry Keys"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use Windows PowerShell to create and manage registry keys.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today I am happy to provide you with an excerpt from my book, <a href=\"http:\/\/www.amazon.com\/Windows-PowerShell-3-0-Step\/dp\/0735663394\/ref=tmm_pap_title_0?ie=UTF8&amp;qid=1383770622&amp;sr=8-1\" target=\"_blank\">Windows PowerShell 3.0 Step by Step<\/a>, published by Microsoft Press.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8512.Step%20by%20Step%20book.jpg\"><img decoding=\"async\" title=\"Image of book cover\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8512.Step%20by%20Step%20book.jpg\" alt=\"Image of book cover\" \/><\/a><\/p>\n<p class=\"Readeraidonly\" style=\"padding-left: 30px\"><strong>Note&nbsp;<\/strong> 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 <a href=\"http:\/\/support.microsoft.com\/kb\/322756\" target=\"_blank\">article 322756<\/a> in the Microsoft Knowledge Base. For general information about working with the registry, see <a href=\"http:\/\/support.microsoft.com\/kb\/310516\" target=\"_blank\">article 310516<\/a>.<\/p>\n<p>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 <strong>New-Item<\/strong> cmdlet. In addition, you might use the <strong>Test-Path<\/strong> 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 <strong>Push-Location<\/strong>, <strong>Set-Location<\/strong>, and <strong>Pop-Location<\/strong> cmdlets. This is, of course, the long way of doing things&hellip;<\/p>\n<p><strong>Just the steps<\/strong><\/p>\n<ol>\n<li>Store the current working location by using the <strong>Push-Location<\/strong> cmdlet.<\/li>\n<li>Change the current working location to the appropriate registry drive by using the <strong>Set-Location<\/strong> cmdlet.<\/li>\n<li>Use the <strong>Test-Path<\/strong> cmdlet to determine if the registry key already exists.<\/li>\n<li>Use the <strong>New-Item<\/strong> cmdlet to create the new registry key.<\/li>\n<li>Use the <strong>Pop-Location<\/strong> cmdlet to return to the starting working location.<\/li>\n<\/ol>\n<p>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.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Push-Location<br \/> Set-Location HKCU:<br \/> Test-Path .\\Software\\test<br \/> New-Item -Path .\\Software -Name test<br \/> Pop-Location<\/p>\n<p>The commands and the associated output from the commands are shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2248.hsg-12-10-13-1.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2248.hsg-12-10-13-1.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h3>The short way to create a new registry key<\/h3>\n<p>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 <strong>Test-Path<\/strong> 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 <strong>Force<\/strong><em> <\/em>parameter.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note&nbsp;<\/strong> How you choose to deal with an already existing registry key is one of those design decisions<em> <\/em>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, <a href=\"http:\/\/www.microsoft.com\/learning\/en-us\/book.aspx?ID=13347\" target=\"_blank\">Windows PowerShell 2.0 Best Practices<\/a><em>. <\/em><\/p>\n<p>The following example creates a new registry key named <strong>test<\/strong> 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 <strong>Force<\/strong><em> <\/em>switched parameter, the command overwrites the HKCU:\\Software\\test registry key if it already exists.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">New-Item -Path HKCU:\\Software -Name test &ndash;Force<\/p>\n<p><strong>Just the steps: The short way to create a new registry key<\/strong><\/p>\n<ol>\n<li>Include the full path to the registry key to create.<\/li>\n<li>Use the <strong>Force<\/strong><em> <\/em>parameter to overwrite any existing registry key of the same name.<\/li>\n<\/ol>\n<p>In the following image, the first attempt to create a test registry key fails because it already exists. The second command uses the <strong>Force<\/strong><em> <\/em>parameter, which causes the command to overwrite the existing registry key, and therefore it succeeds without creating an error.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8475.hsg-12-10-13-2.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8475.hsg-12-10-13-2.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h3>Setting the default value for the key<\/h3>\n<p>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 <strong>Set-Item<\/strong> cmdlet to assign a default value to the registry key. The steps to accomplish this are shown here:<\/p>\n<p><strong>Just the steps: Assign a default value to an existing registry key<\/strong><\/p>\n<ol>\n<li>Use the <strong>Set-Item<\/strong> cmdlet and supply the complete path to the existing registry key.<\/li>\n<li>Supply the default value in the <strong>Value<\/strong><em> <\/em>parameter of the <strong>Set-Item<\/strong> cmdlet.<\/li>\n<\/ol>\n<p>The following command assigns the value <strong>&ldquo;test key&rdquo;<\/strong> to the default property value of the hsg registry key contained in the HKCU:\\Software location.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Set-Item -Path HKCU:\\Software\\test -Value &#8220;test key&#8221;<\/p>\n<p>Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. Note&nbsp; The registry contains information that is vital to the operation and configuration of your [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,3,4,45],"class_list":["post-2446","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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. Note&nbsp; The registry contains information that is vital to the operation and configuration of your [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2446","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=2446"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2446\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=2446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}