Using Transactions with PowerShell Registry Provider


Summary: Microsoft Scripting Guy, Ed Wilson, talks about using transactions with the Windows PowerShell Registry provider.

Hey, Scripting Guy! Question Hey, Scripting Guy! I was looking at the various providers and their capabilities (see Find and Use Windows PowerShell Providers). I see that the Registry provider says it does transactions. What’s that all about?

—JS

Hey, Scripting Guy! Answer Hello JS,

Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sipping a cup of tea from some tea that Windows PowerShell MVP, Sean Kearney, brought me from Ottawa. There just happens to be an awesome tea shop there, and Sean volunteered to bring me some. WooHoo!!

A transaction is a unit of work that must complete completely, or it must fail and roll back completely. An example of this might be transferring money from one bank account to another bank account. The steps might include:

  1. Access the first bank account.
  2. Remove money from the first bank account.
  3. Access the second bank account.
  4. Add money from the first bank account to the second account.

Obviously, I would not want the first two steps to complete and the last two steps to fail.

Anyway JS, yes indeed, the Registry provider supports transactions. In fact, it has supported transactions since Windows PowerShell 2.0. But the operating system must be Windows Vista or later. Theoretically, any provider could support transactions, but the only default provider that does support transactions is the Registry provider. I can use the following command to find providers that support transactions:

PS C:\> Get-PSProvider | ? capabilities -match 'transactions'

Name                 Capabilities                                  Drives                                      

—-                 ————                                  ——                                      

Registry             ShouldProcess, Transactions                   {HKLM, HKCU}                                

To begin a transaction, I use the Start-Transaction cmdlet. When I do this, it appears as if nothing happens. This command is shown here:

PS C:\> Start-Transaction

PS C:\> 

The next thing I do is type in the command that I want to perform. Each command that I type does not have to be part of the transaction, but if I want it to be included in the transaction, I must use the –UseTransaction switch. My current location does not have to be one of the Registry drives, but the path needs to resolve to a registry drive (or to another drive that is exposed by another Windows PowerShell provider that supports transactions).

In the output shown here, I start a transaction and create a registry key:

PS C:\> Start-Transaction

PS C:\> New-Item -Name trans -Path HKCU:\Software\ScriptingGuys -Value "transaction" -UseTransaction

    Hive: HKEY_CURRENT_USER\Software\ScriptingGuys

Name                           Property                                                                        

—-                           ——–                                                                        

trans                                                                                                          

It looks like the registry key is already created. But because I have not completed my transaction, I know that it has not actually been created yet. I can verify this by opening the Registry Editor and looking up my registry key. I can see that the registry key is not there:

Image of menu

I can create additional registry keys or complete my transaction.

To complete the transaction, I call the Complete-Transaction cmdlet. One thing that is perhaps a bit disconcerting, is that nothing comes back when I call Complete-Transaction. This is shown here:

PS C:\> Complete-Transaction

PS C:\> 

So once again, I pop back over to the Registry Editor. I hit the Refresh button, and now I can see that the transaction did complete:

Image of menu

The commands and the output from the commands are shown here:

Image of command output

JS, that is it. It is really easy to do transactions with the Windows PowerShell Registry provider. Provider Week will continue tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Comments (0)

Skip to main content