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?
— SK
Hello SK,
Microsoft Scripting Guy Ed Wilson here. The rains came to Charlotte, North Carolina, and washed away the remnants of our recent snowfall (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 Earl Grey tea with lemon grass, and a cinnamon stick. I am also munching on some oatmeal and some apple walnut cookies 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 Fisherman’s Wharf in San Francisco, California, when I was out there teaching a VBScript workshop. I took the following picture that day.
To use a transaction in the registry, you must first start a transaction. To do this, use the Start-Transaction cmdlet as seen here:
PS C:> Start-Transaction
Suggestion [1,Transactions]: Once a transaction is started, only commands that get called with the -UseTransact
become part of that transaction.
PS C:>
It is rather annoying that the suggestion always pops up when you start a new transaction. I have tried piping to the Out-Null cmdlet, or redirecting to $null, but the message is still displayed. There does not appear to be a parameter that can suppress this message.
After you have started the transaction, you can use the transaction to create a new registry key. To use the transaction, you use the –useTransaction switched parameter on the cmdlet. The following command will create a new registry key when the transaction is completed:
PS C:> New-Item -Name test2 -Path HKCU:SoftwareScriptingGuys -Value testValue -UseTransaction
Hive: HKEY_CURRENT_USERSoftwareScriptingGuys
SKC VC Name Property
— — —- ——–
0 1 test2 {(default)}
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.
To complete the transaction, use the Complete-Transaction cmdlet. You can then use the Get-Item cmdlet to see if the new registry key has been created. This is seen here:
PS C:> Complete-Transaction
PS C:> Get-Item HKCU:SoftwareScriptingGuystest2
Hive: HKEY_CURRENT_USERSoftwareScriptingGuys
SKC VC Name Property
— — —- ——–
0 1 test2 {(default)}
SK, because you specifically mentioned remote registry operations, you first need to create a remote Windows PowerShell session. To do this, use the Enter-PSSession cmdlet and specify the computer name. This is shown here:
PS C:> Enter-PSSession -ComputerName c2
Next you need to start a transaction by using the Start-Transaction cmdlet, and then change your working location to a registry drive. This is shown here:
[c2]: PS C:> Start-Transaction
[c2]: PS C:> sl hkcu:
After you have moved to the new location, use the New-Item cmdlet to create a new registry key. Remember, you must use the –UseTransaction switched parameter, or the registry key will be created immediately. This is shown here:
[c2]: PS HKCU:> New-Item ScriptingGuys -UseTransaction
Hive: HKEY_CURRENT_USER
SKC VC Name Property
— — —- ——–
0 0 ScriptingGuys {}
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 Get-Item cmdlet with the –UseTransaction switched parameter. This will show you the results after the transaction is completed. This is seen here:
[c2]: PS HKCU:> Get-Item ScriptingGuys -UseTransaction
Hive: HKEY_CURRENT_USER
SKC VC Name Property
— — —- ——–
0 0 ScriptingGuys {}
To complete the transaction, use the Complete-Transaction cmdlet, and exit from the remote Windows PowerShell session. This is shown here:
[c2]: PS HKCU:> Complete-Transaction
[c2]: PS HKCU:> exit
PS C:>
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.
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…wait a minute…
If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson and Craig Liebendorfer, Scripting Guys
0 comments