PowerShell 2.0: One Cmdlet at a Time 81 Undo-Transaction

Continuing the series looking at new cmdlets available in PowerShell 2.0. This time we look at the Undo-Transaction cmdlet.

What can I do with it?

PowerShell 2.0 introduces new functionality in the form of transactions. By grouping together a set of commands to form a transaction they can either all be committed or all rolled back depending on success.

Undo-Transaction rolls back the active transaction.

Example:

A good example of a possible use for transactions is within the registry. Change directory into the registry provider. Begin a new transaction and use the New-Item and New-ItemProperty cmdlets to potentially create entries within the registry. Use Get-Transaction to view details of the current transaction.

cd HKLM:\Software Start-Transaction New-Item Test -UseTransaction New-ItemProperty Test -Name TestKey -Value 1000 -UseTransaction Get-Transaction

You will notice that there is currently 1 subscriber and the status is Active.

Start a new transaction, use the New-ItemProperty cmdlets to potentially create another new entry within the registry and use Get-Transaction to view details of the current transaction.

Start-Transaction New-ItemProperty Test -Name TestKey2 -Value 2000 -UseTransaction Get-Transaction

You will notice that there are now 2 subscribers and the status is still Active.

Now use Undo-Transaction to roll back the changes and  Get-Transaction to view details of the current transaction

Undo-Transaction Get-Transaction

Notice that it has rolled back the changes for both transactions and the status is now RolledBack.

How could I have done this in PowerShell 1.0?

Transactional functionality was not available in PowerShell 1.0.

1000 things 1% better!