{"id":9471,"date":"2012-05-10T00:01:00","date_gmt":"2012-05-10T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/05\/10\/use-powershell-to-create-new-registry-keys-on-remote-systems\/"},"modified":"2012-05-10T00:01:00","modified_gmt":"2012-05-10T00:01:00","slug":"use-powershell-to-create-new-registry-keys-on-remote-systems","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-new-registry-keys-on-remote-systems\/","title":{"rendered":"Use PowerShell to Create New Registry Keys on Remote Systems"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use Windows PowerShell to create new registry keys on remote systems by using remoting.<\/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 need to create registry keys on a number of remote servers. I do not want to use the Registry Editor to do this because the servers all have the firewall enabled, and I do not want to enable the remote registry service on these machines. Is there a way I can use Windows PowerShell to create these registry keys? I really do not want to use remote desktop to do this.<\/p>\n<p>&mdash;YH<\/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 YH,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. I just finished a team meeting where we were talking about documentation for the Windows Server &ldquo;8&rdquo; Beta release&mdash;really exciting stuff. I can tell you that Microsoft is the most exciting company for which I have ever worked. It is everything I hoped for when I joined nearly 11 years ago&mdash;and even more. I am dying to start writing about Windows PowerShell&nbsp;3.0 and Windows Server &ldquo;8&rdquo; Beta, but I imagine that most IT Pros have not yet deployed it to their systems, so I will forgo that until the product actually ships.<\/p>\n<p>I am sipping a cup of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Darjeeling_tea\" target=\"_blank\">Darjeeling tea<\/a> with some lemon grass and a cinnamon stick, accompanied by a slice of 90 percent cacao chocolate. It is the perfect afternoon snack. This week is turning out to be a lot of fun. Tonight, the Scripting Wife and I are having dinner with one of the members of the Charlotte Windows PowerShell Users Group. We will be talking about upcoming user group presentations. It is always a lot of fun to get together with fellow Windows PowerShell geeks because the conversations are usually $cool.<\/p>\n<p><b>Note<\/b>&nbsp;&nbsp;&nbsp;This is the third blog in a series of Hey, Scripting Guy! Blogs that discuss using the Registry provider. The first blog, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/05\/07\/use-the-powershell-registry-provider-to-simplify-registry-access.aspx\" target=\"_blank\">Use the Registry Provider to Simply Registry Access<\/a><i>,&nbsp;<\/i>posted on Monday. Tuesday I discussed <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/05\/08\/use-powershell-to-back-up-system-state-prior-to-making-changes.aspx\" target=\"_blank\">using the *restore* cmdlets<\/a> to perform a system state backup of a computer prior to manipulating the registry. On Wednesday I talked about <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/05\/09\/use-powershell-to-easily-create-new-registry-keys.aspx\" target=\"_blank\">creating new registry keys and assigning default values<\/a>. For additional information about working with the registry via Windows PowerShell, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/operating+system+\/registry\/\" target=\"_blank\">see this collection of blogs<\/a>.<\/p>\n<p>YH, there are several ways to create registry keys on remote systems. One way is to use the .NET Framework classes and another way is to use WMI. By far, the easiest way is to combine using the Windows PowerShell Registry provider with Windows PowerShell remoting. This is because Windows PowerShell remoting uses the firewall friendly WSMan protocol. In fact, inside a single forest, single domain with Windows Server &ldquo;8&rdquo; Beta, remoting just works. There are no configuration changes required to enable Windows PowerShell remoting in Windows Server &ldquo;8&rdquo; Beta.<\/p>\n<h2>Entering a remote session to create a new registry key<\/h2>\n<p>If you only have a single computer upon which you need to create one or more new registry keys, it is probably easiest to enter a remote Windows PowerShell session. Doing this provides you with the equivalent experience of opening the Windows PowerShell console and working in an interactive fashion. There are several steps involved, but they are not too difficult.<\/p>\n<p>Only the steps&hellip;<\/p>\n<p>Entering a remote Windows PowerShell session to create a new registry key:<\/p>\n<ol>\n<li>Use the <b>Get-Credential<\/b> cmdlet to obtain a credential object with rights on the remote computer. Store the returned credential object in a variable.<\/li>\n<li>Use the <b>Enter-PSSession<\/b> cmdlet to enter a remote Windows PowerShell session on the target computer.<\/li>\n<li>Use the <b>New-Item<\/b> cmdlet to create the new registry key.<\/li>\n<li>Use the <b>Exit<\/b> command to leave the remote Windows PowerShell session.<\/li>\n<\/ol>\n<p>The commands to obtain credentials, enter a Windows PowerShell session, create a new registry key, and leave the Windows PowerShell session are shown here.<\/p>\n<p style=\"padding-left: 30px\">$credential = Get-Credential -Credential iammred\\administrator<\/p>\n<p style=\"padding-left: 30px\">Enter-PSSession -ComputerName sql1 -Credential $credential<\/p>\n<p style=\"padding-left: 30px\">New-Item -Path HKCU:\\Software -Name HSG -Value &#8220;my default value&#8221;<\/p>\n<p style=\"padding-left: 30px\">Exit<\/p>\n<p>The use of these commands and the associated output are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3858.hsg-5-10-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3858.hsg-5-10-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I use Remote Desktop Protocol (RDP) to connect to the SQL1 server to verify creation of the registry key. Keep in mind that when you make a remote connection and create keys on the HKCU drive, the user account that makes the remote connection is the current user who will hold the newly created registry key. The newly created registry key is shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8547.hsg-5-10-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8547.hsg-5-10-12-02.png\" alt=\"Image of Registry Editor\" title=\"Image of Registry Editor\" \/><\/a><\/p>\n<h2>Creating remote registry keys on multiple computers<\/h2>\n<p>Entering a remote Windows PowerShell session to create a registry key on multiple computers is a tedious and time-consuming process. To perform a command on multiple machines, it is better to use the <b>Invoke-Command<\/b> cmdlet.<\/p>\n<p>Only the steps&hellip;<\/p>\n<p>Using the <b>Invoke-Command<\/b> cmdlet to create remote registry keys:<\/p>\n<ol>\n<li>Store the server names in a variable.<\/li>\n<li>Store the connection credentials in a variable (use the <b>Get-Credential<\/b> cmdlet to obtain the credentials).<\/li>\n<li>Use the <b>Invoke-Command<\/b> cmdlet to run the command against the remote machines. Place the command to be run in the <i>ScriptBlock <\/i>parameter.<\/li>\n<\/ol>\n<p>The following commands create a new registry key on the HKCU drive on three different servers.<\/p>\n<p style=\"padding-left: 30px\">$servers = &#8220;hyperv1&#8243;,&#8221;hyperv2&#8243;,&#8221;hyperv3&#8221;<\/p>\n<p style=\"padding-left: 30px\">$credential = Get-Credential -Credential iammred\\administrator<\/p>\n<p style=\"padding-left: 30px\">Invoke-Command -ComputerName $servers -Credential $credential -ScriptBlock {New-Item -Path HKCU:\\Software -Name hsg -Value &#8220;scripted default value&#8221;}<\/p>\n<p>The commands and the output associated with running the commands are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6378.hsg-5-10-12-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6378.hsg-5-10-12-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The returning output from the <b>Invoke-Command<\/b> cmdlet displays the computer name and illustrates the newly created registry key. However, if you wish further confirmation that the registry keys created properly, it is easy to change the <b>New-Item<\/b> command to <b>Test-Path<\/b>. Because the server names reside in a variable, in addition to the credentials, using <b>Test-Path<\/b> inside the script block of the <b>Invoke-Command<\/b> cmdlet is trivial. Here is the revised command.<\/p>\n<p style=\"padding-left: 30px\">Invoke-Command -ComputerName $servers -Credential $credential -ScriptBlock {Test-Path -Path HKCU:\\Software\\hsg}<\/p>\n<p>The command and its associated output are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2086.hsg-5-10-12-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2086.hsg-5-10-12-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Creating property values<\/h2>\n<p>It is unusual to have a registry key that has only a default property value. In fact, most registry keys contain multiple property values. To create a new property value, use the <b>New-ItemProperty<\/b> cmdlet. The following command creates a new property value named NewProperty under the previously created HSG registry key.<\/p>\n<p style=\"padding-left: 30px\">New-ItemProperty -Path HKCU:\\Software\\hsg -Name NewProperty -Value &#8220;New PropertyValue&#8221;<\/p>\n<p>The newly created registry key property is shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1401.hsg-5-10-12-05.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1401.hsg-5-10-12-05.png\" alt=\"Image of Registry Editor\" title=\"Image of Registry Editor\" \/><\/a><\/p>\n<h2>Getting registry property values<\/h2>\n<p>When run, the command returns information about the registry key property. In fact, this is the same information that is obtained by using the <b>Get-ItemProperty<\/b> cmdlet. The use of the <b>Get-ItemProperty<\/b> cmdlet and the associated output from the command are shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-ItemProperty -path HKCU:\\Software\\hsg -Name newproperty<\/p>\n<p style=\"padding-left: 30px\">PSPath&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Microsoft.PowerShell.Core\\Registry::HKEY_CURRENT_USER\\Software\\hsg<\/p>\n<p style=\"padding-left: 30px\">PSParentPath : Microsoft.PowerShell.Core\\Registry::HKEY_CURRENT_USER\\Software<\/p>\n<p style=\"padding-left: 30px\">PSChildName&nbsp; : hsg<\/p>\n<p style=\"padding-left: 30px\">PSDrive&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : HKCU<\/p>\n<p style=\"padding-left: 30px\">PSProvider&nbsp;&nbsp; : Microsoft.PowerShell.Core\\Registry<\/p>\n<p style=\"padding-left: 30px\">NewProperty&nbsp; : New PropertyValue<\/p>\n<p>The newly created registry property appears in the output under the name of the property. This means that to display only the value of the registry property requires essentially naming the property twice. The first time the registry property appears, it is the name of the registry property to obtain; the second time, it is the property to retrieve. This command and the associated output are shown here.<\/p>\n<p style=\"padding-left: 30px\">&nbsp;PS C:\\&gt; (Get-ItemProperty -path HKCU:\\Software\\hsg -Name newproperty).newProperty<\/p>\n<p style=\"padding-left: 30px\">New PropertyValue<\/p>\n<p>YH, that is all there is to using Windows PowerShell to create registry keys on remote systems. Registry Week will continue tomorrow when I will talk about enumerating registry key properties.<\/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>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to create new registry keys on remote systems by using remoting. &nbsp;Hey, Scripting Guy! I need to create registry keys on a number of remote servers. I do not want to use the Registry Editor to do this because the servers all have the firewall enabled, and I [&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,31,26,3,4,45],"class_list":["post-9471","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-operating-system","tag-registry","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to create new registry keys on remote systems by using remoting. &nbsp;Hey, Scripting Guy! I need to create registry keys on a number of remote servers. I do not want to use the Registry Editor to do this because the servers all have the firewall enabled, and I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9471","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=9471"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9471\/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=9471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=9471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=9471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}