PowerShell 2.0: One Cmdlet at a Time 78 Start-Transaction

Continuing the series looking at new cmdlets available in PowerShell 2.0. This time we look at the Start-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. Both cmdlets and providers can support transactions, cmdlets will have a UseTransaction parameter. To identify which cmdlets support transactions run the following:

Get-Command | Where-Object {$_.Definition -match ‘UseTransaction’}

and for providers:

Get-PSProvider | Where-Object {$_.Capabilities -like ‘*transactions*’}

Start-Transaction begins a 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.

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

You will notice that since we have not yet completed the transaction no changes have yet been made in the registry.

How could I have done this in PowerShell 1.0?

Transactional functionality was not available in PowerShell 1.0.

1000 things 1% better!