Transactions

Manage transacted operations
This feature enables you to start
a transaction, indicate which commands are part of the
transaction and then either commit or roll back (cancel) the transaction.

Commands in a transaction are managed as a unit,
either all commands are committed, or all commands are rolled
back. To participate in a transaction, both the cmdlet and the provider
must support transactions. In some cases, (e.g. the windows registry) transaction support requires Vista or above.

The -UseTransaction parameter (or its alias -usetx) Cmdlets that can support transactions have a -UseTransaction
parameter. This parameter can be used only when the session contains an
active transaction and includes the command in the active
transaction.

The Transactions Object

Transactions are represented in Windows PowerShell by System.Management.Automation.Transaction

The object has the following 3 properties:

RollbackPreference: Error, TerminatingError, or Never
Set the rollback preference with Start-Transaction.

Status: Active, Committed, and RolledBack
The current status of the transaction.

SubscriberCount: the number of subscribers to the transaction.

Multiple
transactions can be in progress in the same session at the same
time, (nested) but only the most-recently started transaction is active.

INDEPENDENT TRANSACTIONS

If you start a transaction while another transaction is in progress, PowerShell will (by default) add a "subscriber" to the current transaction rather than start a new transaction.

When a transaction has multiple subscribers, a single
Undo-Transaction command will roll back the entire
transaction for all subscribers. However,
you must still enter a Complete-Transaction command for every subscriber.

You can start a transaction that is independent of the
current transaction by using the -Independent parameter of
Start-Transaction.

When an independent transaction is finished (committed or rolled
back), the original transaction becomes the active transaction
again.

LOCKING

Unlike a database transaction, PowerShell transactions do not 'lock' any data, so other applications or other users could potentially change the data while a PowerShell transaction is running.