{"id":6911,"date":"2015-04-02T00:01:00","date_gmt":"2015-04-02T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/04\/02\/update-or-add-registry-key-value-with-powershell\/"},"modified":"2019-02-18T10:30:03","modified_gmt":"2019-02-18T17:30:03","slug":"update-or-add-registry-key-value-with-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/update-or-add-registry-key-value-with-powershell\/","title":{"rendered":"Update or Add Registry Key Value with PowerShell"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to update or add a registry key value.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! I am having a problem trying to update the registry. I am using the <b>New-ItemProperty<\/b> cmdlet, but it fails if the registry key does not exist. I added the <b>&ndash;Force<\/b> parameter, but it still will not create the registry key. The error message says that it cannot find the path because it does not exist. Is there something I am not doing? I am including my script so you can see what is going on. Help me please.<\/p>\n<p>&mdash;DC<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello DC,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. In your script, you are using the <b>New-ItemProperty<\/b> cmdlet to attempt to update a registry key property value. When the registry key property exists, your script works. But when it does not, it fails. Here is a version of your script:<\/p>\n<p style=\"margin-left:30px\">$registryPath = &quot;HKCU:\\Software\\ScriptingGuys\\Scripts&quot;<\/p>\n<p style=\"margin-left:30px\">$Name = &quot;Version&quot;<\/p>\n<p style=\"margin-left:30px\">$value = &quot;1&quot;<\/p>\n<p style=\"margin-left:30px\">New-ItemProperty -Path $registryPath -Name $name -Value $value `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; -PropertyType DWORD -Force | Out-Null<\/p>\n<p>And here is the error message that appears when the registry key does not exist:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-4-2-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-4-2-15-01.png\" alt=\"Image of error message\" title=\"Image of error message\" \/><\/a><\/p>\n<p>You need to test for the existence of the registry key. If the registry key does not exist, then you need to create the registry key, and then create the registry key property value. The first thing I like to do is to create the path to the registry key, then specify the property name and the value I want to assign. This consists of three variables as shown here:<\/p>\n<p style=\"margin-left:30px\">$registryPath = &quot;HKCU:\\Software\\ScriptingGuys\\Scripts&quot;<\/p>\n<p style=\"margin-left:30px\">$Name = &quot;Version&quot;<\/p>\n<p style=\"margin-left:30px\">$value = &quot;1&quot;<\/p>\n<p>Now I can use the <b>Test-Path<\/b> cmdlet to see if the registry key exists. To do this, I use the <b>If<\/b> statement, and I look for the registry key that is NOT existing. I use the explanation point ( <b>! <\/b>)<b> <\/b>as the not operator. I need to put the <b>Test-Path<\/b> statement in a pair of parentheses so that I am &quot;NOTing&quot; the condition. This is shown here:<\/p>\n<p style=\"margin-left:30px\">IF(!(Test-Path $registryPath))<\/p>\n<p>You may wonder why I cannot use <b>Test-Path<\/b> to verify that the registry key property does not exist. After all, that is what the script is all about in the first place. The reason, is that <b>Test-Path<\/b> does not know how to work with registry key property values. It will attempt to work, but it does not.<\/p>\n<p>Here is an example of doing that:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Test-Path HKCU:\\Software\\ScriptingGuys\\scripts<\/p>\n<p style=\"margin-left:30px\">True<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Test-Path HKCU:\\Software\\ScriptingGuys\\scripts\\version<\/p>\n<p style=\"margin-left:30px\">False<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; (Get-ItemProperty -Path HKCU:\\Software\\ScriptingGuys\\Scripts -Name version).version<\/p>\n<p style=\"margin-left:30px\">1<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Test-Path HKCU:\\Software\\ScriptingGuys\\scripts\\version -PathType Leaf<\/p>\n<p style=\"margin-left:30px\">False<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Test-Path HKCU:\\Software\\ScriptingGuys\\scripts\\version -PathType Any<\/p>\n<p style=\"margin-left:30px\">False<\/p>\n<p>After I verify that the registry key exists, I use the <b>New-ItemProperty<\/b> cmdlet to create or update my registry key property value:<\/p>\n<p style=\"margin-left:30px\">New-Item -Path $registryPath -Force | Out-Null<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; New-ItemProperty -Path $registryPath -Name $name -Value $value `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; -PropertyType DWORD -Force | Out-Null<\/p>\n<p>If the registry key already exists, there is no need to attempt to create it again, so I create the registry key property value. As shown here, this code appears in the <b>ELSE<\/b> condition of the statement:<\/p>\n<p style=\"margin-left:30px\">ELSE {<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; New-ItemProperty -Path $registryPath -Name $name -Value $value `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; -PropertyType DWORD -Force | Out-Null}<\/p>\n<p>Here is the complete script:<\/p>\n<p style=\"margin-left:30px\">$registryPath = &quot;HKCU:\\Software\\ScriptingGuys\\Scripts&quot;<\/p>\n<p style=\"margin-left:30px\">$Name = &quot;Version&quot;<\/p>\n<p style=\"margin-left:30px\">$value = &quot;1&quot;<\/p>\n<p style=\"margin-left:30px\">IF(!(Test-Path $registryPath))<\/p>\n<p style=\"margin-left:30px\">&nbsp; {<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; New-Item -Path $registryPath -Force | Out-Null<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; New-ItemProperty -Path $registryPath -Name $name -Value $value `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; -PropertyType DWORD -Force | Out-Null}<\/p>\n<p style=\"margin-left:30px\">&nbsp;ELSE {<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; New-ItemProperty -Path $registryPath -Name $name -Value $value `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; -PropertyType DWORD -Force | Out-Null}<\/p>\n<p>I can use the <b>Registry Editor<\/b> to verify that my registry key property value exists with the proper value:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-4-2-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-4-2-15-02.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>Or, I can use the Get-ItemProperty cmdlet. Such a command is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; (Get-ItemProperty -Path HKCU:\\Software\\ScriptingGuys\\Scripts -Name version).version<\/p>\n<p style=\"margin-left:30px\">1<\/p>\n<p>I could use <b>Get-ItemProperty<\/b> to verify if a registry key property value exists. If the registry key property does not exist, the error message is very specific. It is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; (Get-ItemProperty -Path HKCU:\\Software\\ScriptingGuys\\Scripts -Name version).version<\/p>\n<p style=\"margin-left:30px\">Get-ItemProperty : Property version does not exist at path HKEY_CURRENT_USER\\Software\\ScriptingGuys\\Scripts.<\/p>\n<p style=\"margin-left:30px\">At line:1 char:2<\/p>\n<p style=\"margin-left:30px\">+ (Get-ItemProperty -Path HKCU:\\Software\\ScriptingGuys\\Scripts -Name version).vers &#8230;<\/p>\n<p style=\"margin-left:30px\">+&nbsp; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : InvalidArgument: (version:String) [Get-ItemProperty], PSArgumentException<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : System.Management.Automation.PSArgumentException,Microsoft.PowerShell.Commands.Ge<span style=\"font-size:12px\">tItemPropertyCommand<\/span><\/p>\n<p>If the registry key itself does not exist, I get a different error message. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; (Get-ItemProperty -Path HKCU:\\Software\\ScriptingGuys\\Scripts -Name version).version<\/p>\n<p style=\"margin-left:30px\">Get-ItemProperty : Cannot find path &#039;HKCU:\\Software\\ScriptingGuys\\Scripts&#039; because it does not exist.<\/p>\n<p style=\"margin-left:30px\">At line:1 char:2<\/p>\n<p style=\"margin-left:30px\">+ (Get-ItemProperty -Path HKCU:\\Software\\ScriptingGuys\\Scripts -Name version).vers &#8230;<\/p>\n<p style=\"margin-left:30px\">+&nbsp; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : ObjectNotFound: (HKCU:\\Software\\ScriptingGuys\\Scripts:String) [Get-ItemProperty],<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; ItemNotFoundException<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand<\/p>\n<p>The first error message complains about the PROPERTY, the second error complains about the PATH. So, two different error messages are returned depending on the issue. Therefore, I could implement structured error handling, detect the specific error, and then correct it as appropriate.<\/p>\n<p>But that would be more complicated than I want to do for making simple registry changes. If you have such a need, you may want to check out this collection of <a href=\"https:\/\/social.technet.microsoft.com\/Search\/en-US?query=structured%20error%20handling&amp;rn=Hey,%20Scripting%20Guy!%20Blog&amp;rq=site:blogs.technet.com\/b\/heyscriptingguy\/&amp;beta=0&amp;ac=5\" target=\"_blank\">Hey, Scripting Guy! Blog posts<\/a>, where I talk about structured error handling in Windows PowerShell scripts.<\/p>\n<p>DC, that is all that is needed to fix your registry script. Troubleshooting Week will continue tomorrow when I will talk about more cool stuff.<span style=\"font-size:12px\">&nbsp;<\/span><\/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><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to update or add a registry key value. &nbsp;Hey, Scripting Guy! I am having a problem trying to update the registry. I am using the New-ItemProperty cmdlet, but it fails if the registry key does not exist. I added the &ndash;Force parameter, but it [&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":[26,3,4,134,45],"class_list":["post-6911","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-registry","tag-scripting-guy","tag-scripting-techniques","tag-troubleshooting","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to update or add a registry key value. &nbsp;Hey, Scripting Guy! I am having a problem trying to update the registry. I am using the New-ItemProperty cmdlet, but it fails if the registry key does not exist. I added the &ndash;Force parameter, but it [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/6911","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=6911"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/6911\/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=6911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=6911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=6911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}