{"id":51213,"date":"2010-02-23T00:01:00","date_gmt":"2010-02-23T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/02\/23\/hey-scripting-guy-how-can-i-change-the-registry-with-windows-powershell-2-0-transactions\/"},"modified":"2010-02-23T00:01:00","modified_gmt":"2010-02-23T00:01:00","slug":"hey-scripting-guy-how-can-i-change-the-registry-with-windows-powershell-2-0-transactions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-change-the-registry-with-windows-powershell-2-0-transactions\/","title":{"rendered":"Hey, Scripting Guy! How Can I Change the Registry with Windows PowerShell 2.0 Transactions?"},"content":{"rendered":"<p class=\"MsoNormal\"><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a>&nbsp;<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/p>\n<p class=\"MsoNormal\">Hey, Scripting Guy! I have been using Windows PowerShell to manipulate the registry since Windows PowerShell 1.0 came out. With Windows PowerShell 2.0, I can now edit the registry on a remote computer. How can I use transactions with the registry provider so that I can ensure that my changes to the registry do not fail in midstream?<\/p>\n<p class=\"MsoNormal\">&#8212; SK<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">Hello SK, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. The rains came to Charlotte, North Carolina, and washed away the remnants of our recent <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2010\/02\/06\/hey-scripting-guy-february-6-2010.aspx\">snowfall<\/a> (the picture is actually of our first snowfall, but the second was like the first). On cold rainy days, I like to have a pot of tea near my desk. This afternoon it is <a href=\"http:\/\/en.wikipedia.org\/wiki\/Earl_Grey_tea\">Earl Grey tea<\/a> with <a href=\"http:\/\/en.wikipedia.org\/wiki\/Cymbopogon\"><font face=\"Segoe\">lemon grass<\/font><\/a>, and a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Cinnamon\"><font face=\"Segoe\">cinnamon stick<\/font><\/a>. I am also munching on some oatmeal and some apple walnut <a href=\"http:\/\/en.wikipedia.org\/wiki\/Oatmeal_cookie\"><font face=\"Segoe\">cookies<\/font><\/a> the Scripting Wife baked. She uses the apples and avoids adding any extra sugar to the mix. The cool damp weather reminds me of a day I spent at <a href=\"http:\/\/en.wikipedia.org\/wiki\/Fisherman%27s_Wharf,_San_Francisco,_California\"><font face=\"Segoe\">Fisherman&rsquo;s Wharf<\/font><\/a> in San Francisco, California, when I was out there teaching a VBScript workshop. I took the following picture that day. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of a seal on Fisherman's Wharf in San Francisco, California\" alt=\"Image of a seal on Fisherman's Wharf in San Francisco, California\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/february\/hey0223\/hsg-02-23-10-01.jpg\" width=\"600\" height=\"450\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">To use a transaction in the registry, you must first start a transaction. To do this, use the <b>Start-Transaction<\/b> cmdlet as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Start-Transaction<\/p>\n<p>Suggestion [1,Transactions]: Once a transaction is started, only commands that get called with the -UseTransact<br \/>become part of that transaction.<br \/>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">It is rather annoying that the suggestion always pops up when you start a new transaction. I have tried piping to the <b>Out-Null<\/b> cmdlet, or redirecting to <b>$null<\/b>, but the message is still displayed. There does not appear to be a parameter that can suppress this message. <\/p>\n<p class=\"MsoNormal\">After you have started the transaction, you can use the transaction to create a new registry key. To use the transaction, you use the <b>&ndash;useTransaction<\/b> switched parameter on the cmdlet. The following command will create a new registry key when the transaction is completed:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; New-Item -Name test2 -Path HKCU:SoftwareScriptingGuys -Value testValue -UseTransaction<\/p>\n<p><span>&nbsp;&nbsp;&nbsp; <\/span>Hive: HKEY_CURRENT_USERSoftwareScriptingGuys<\/p>\n<p>SKC<span>&nbsp; <\/span>VC Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Property<br \/>&#8212;<span>&nbsp; <\/span>&#8212; &#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8211;<br \/><span>&nbsp; <\/span>0<span>&nbsp;&nbsp; <\/span>1 test2<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{(default)}<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">At this point, the transaction has not completed; therefore, the new registry key does not yet appear in the registry. The following image confirms that the key is not yet created.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image showing that key is not yet created\" alt=\"Image showing that key is not yet created\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/february\/hey0223\/hsg-02-23-10-02.jpg\" width=\"600\" height=\"417\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">To complete the transaction, use the <b>Complete-Transaction<\/b> cmdlet. You can then use the <b>Get-Item<\/b> cmdlet to see if the new registry key has been created. This is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Complete-Transaction<br \/>PS C:&gt; Get-Item HKCU:SoftwareScriptingGuystest2<\/p>\n<p><span>&nbsp;&nbsp;&nbsp; <\/span>Hive: HKEY_CURRENT_USERSoftwareScriptingGuys<\/p>\n<p>SKC<span>&nbsp; <\/span>VC Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Property<br \/>&#8212;<span>&nbsp; <\/span>&#8212; &#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8211;<br \/><span>&nbsp; <\/span>0<span>&nbsp;&nbsp; <\/span>1 test2<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{(default)}<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">SK, because you specifically mentioned remote registry operations, you first need to create a remote Windows PowerShell session. To do this, use the <b>Enter-PSSession<\/b> cmdlet and specify the computer name. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Enter-PSSession -ComputerName c2<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Next you need to start a transaction by using the <b>Start-Transaction<\/b> cmdlet, and then change your working location to a registry drive. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">[c2]: PS C:&gt; Start-Transaction<br \/>[c2]: PS C:&gt; sl hkcu:<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">After you have moved to the new location, use the <b>New-Item<\/b> cmdlet to create a new registry key. Remember, you must use the <b>&ndash;UseTransaction<\/b> switched parameter, or the registry key will be created immediately. This is shown here:<\/p>\n<p class=\"CodeBlock\"><font face=\"Lucida Sans Typewriter\"><span>[c2]: PS HKCU:&gt; New-Item ScriptingGuys -UseTransaction<\/p>\n<p><span>&nbsp;&nbsp;&nbsp; <\/span>Hive: HKEY_CURRENT_USER<\/p>\n<p>SKC<span>&nbsp; <\/span>VC Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Property<br \/>&#8212;<span>&nbsp; <\/span>&#8212; &#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8211;<br \/><span>&nbsp; <\/span>0<span>&nbsp;&nbsp; <\/span>0 ScriptingGuys<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{}<\/p>\n<p><br><\/span><\/p>\n<p><\/font><\/p>\n<p class=\"MsoNormal\">If you would like to confirm that the registry key has been accepted, and you would like to see what the change will be when the transaction is completed, use the <b>Get-Item<\/b> cmdlet with the <b>&ndash;UseTransaction<\/b> switched parameter. This will show you the results after the transaction is completed. This is seen here:<\/p>\n<p class=\"CodeBlock\"><span><br \/><font face=\"Lucida Sans Typewriter\">[c2]: PS HKCU:&gt; Get-Item ScriptingGuys -UseTransaction<\/p>\n<p><span>&nbsp;&nbsp;&nbsp; <\/span>Hive: HKEY_CURRENT_USER<\/p>\n<p>SKC<span>&nbsp; <\/span>VC Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Property<br \/>&#8212;<span>&nbsp; <\/span>&#8212; &#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8211;<br \/><span>&nbsp; <\/span>0<span>&nbsp;&nbsp; <\/span>0 ScriptingGuys<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{}<\/p>\n<p><br><\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">To complete the transaction, use the <b>Complete-Transaction<\/b> cmdlet, and exit from the remote Windows PowerShell session. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><br \/><font face=\"Lucida Sans Typewriter\">[c2]: PS HKCU:&gt; Complete-Transaction<br \/>[c2]: PS HKCU:&gt; exit<br \/>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Using the registry editor on the remote computer (via Remote Desktop on Hyper-V), you will see that the registry key was created under the hive of the remote user (in this case nwtradersed). On the remote machine, the nwtradersadministrator is logged in. Therefore, it is necessary to browse to the HKCU for the nwtradersed user. This is shown in the following image. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of registry key's location\" alt=\"Image of registry key's location\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/february\/hey0223\/hsg-02-23-10-03.jpg\" width=\"600\" height=\"375\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">SK, that is all there is to using a transaction to create a local registry key and remote registry key. Join us tomorrow when we will talk about&hellip;wait a minute&hellip; <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/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 class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/b><\/p>\n<p><b><span><\/span><\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I have been using Windows PowerShell to manipulate the registry since Windows PowerShell 1.0 came out. With Windows PowerShell 2.0, I can now edit the registry on a remote computer. How can I use transactions with the registry provider so that I can ensure that my changes to the registry [&hellip;]<\/p>\n","protected":false},"author":595,"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-51213","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>&nbsp; &nbsp; Hey, Scripting Guy! I have been using Windows PowerShell to manipulate the registry since Windows PowerShell 1.0 came out. With Windows PowerShell 2.0, I can now edit the registry on a remote computer. How can I use transactions with the registry provider so that I can ensure that my changes to the registry [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51213","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=51213"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51213\/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=51213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}