CmdID / PowerShell / Cmdlets / Start
Start-Transaction
Starts a transaction. PowerShell reference
From the PowerShell reference 5 parameters
What it does
Starts a transaction. The Start-Transaction cmdlet starts a transaction, which is a series of commands that are managed as a unit. A transaction can be completed, or committed. Alternatively, it can be completely undone, or rolled back, so that any data changed by the transaction is restored to its original state. Because the commands in a transaction are managed as a unit, either all commands are committed or all commands are rolled back. By default, if any command in the transaction generates an error, transactions are rolled back automatically. You can use the RollbackPreference parameter to change this behavior. The cmdlets used in a transaction must be designed to support transactions. Cmdlets that support transactions have a UseTransaction parameter. To perform transactions in a provider, the provider must support transactions. The Windows PowerShell Registry provider in Windows Vista and later versions of the Windows operating system supports transactions. You can also use the Microsoft.PowerShell.Commands.Management.TransactedString class to include expressions in transactions on any version of the Windows system that supports Windows PowerShell. Other Windows PowerShell providers can also support transactions. Only one transaction can be active at a time. If you start a new, independent transaction while a transaction is in progress, the new transaction becomes the active transaction, and you must commit or roll back the new transaction before you make any changes to the original transaction. Start-Transaction cmdlet is one of a set of cmdlets that support the transactions feature in Windows PowerShell. For more information, see about_Transactions.
Start-Transaction [-Timeout <Int32>] [-Independent] [-RollbackPreference <RollbackSeverity>] [-WhatIf] [-Confirm] [<CommonParameters>]
Start and roll back a transaction
Set-Location HKCU:\software
Start-Transaction
New-Item "ContosoCompany" -UseTransaction
New-ItemProperty "ContosoCompany" -Name "MyKey" -Value 123 -UseTransaction
Undo-TransactionIdentifyStart and complete a transaction
Set-Location HKCU:\software
Start-Transaction
New-Item "ContosoCompany" -UseTransaction
New-ItemProperty "ContosoCompany" -Name "MyKey" -Value 123 -UseTransaction
Complete-TransactionIdentifyUse different rollback preferences
Set-Location HKCU:\software
Start-Transaction
New-Item -Path "NoPath" -Name "ContosoCompany" -UseTransaction
New-Item -Path . -Name "ContosoCompany" -UseTransaction
Start-Transaction -RollbackPreference Never
New-Item -Path "NoPath" -Name "ContosoCompany" -UseTransaction
New-Item -Path . -Name "ContosoCompany" -UseTransaction
# Start-Transaction (-RollbackPreference Error)
Start-Transaction
New-Item -Path "NoPath" -Name "ContosoCompany" -UseTransaction
New-Item : The registry key at the specified path does not exist.IdentifyUse this cmdlet while a transaction is in progress
Set-Location HKCU:\software
Start-Transaction
New-Item "ContosoCompany" -UseTransaction
Start-Transaction
Get-Transaction
New-Item "ContosoCompany2" -UseTransaction
Complete-Transaction
Complete-Transaction
Get-TransactionIdentifyStart an independent transaction while one is in progress
Set-Location HKCU:\software
Start-Transaction
New-Item "ContosoCompany" -UseTransaction
Start-Transaction -Independent
Get-Transaction
Undo-Transaction
New-ItemProperty -Path "ContosoCompany" -Name "MyKey" -Value 123 -UseTransaction
Complete-Transaction
Get-ChildItem contoso*
Get-Transaction
RollbackPreference SubscriberCount Status
------------------ --------------- ------
Error 1 Active
Undo-Transaction
New-ItemProperty -Path "ContosoCompany" -Name "MyKey" -Value 123 -UseTransaction
MyKey
-----
123
Complete-Transaction
Get-ChildItem contoso*
Hive: HKEY_CURRENT_USER\Software
SKC VC Name Property
--- -- ---- --------
0 1 MyCompany {MyKey}IdentifyRun commands that are not part of a transaction
Set-Location HKCU:\software
Start-Transaction
New-Item "ContosoCompany1" -UseTransaction
New-Item "ContosoCompany2"
New-Item "ContosoCompany3" -UseTransaction
Get-ChildItem contoso*IdentifyRoll back a transaction that does not finish in a specified time
Start-Transaction -Timeout 2
# Wait two minutes...
Get-Transaction
New-Item HKCU:\Software\ContosoCompany -UseTransaction
Start-Transaction -Timeout 2
# Wait two minutes...
Get-TransactionIdentifyIts page in the reference: Start-Transaction.
Parameters
-IndependentIndicates that this cmdlet starts a transaction that is independent of any transactions in progress. By default, if you use Start-Transaction while another transaction is in progress, a new subscriber is added to the transaction in progress. This parameter has an effect only when a transaction is already in progress in the session.
By default, if you use Start-Transaction while a transaction is in progress, the existing transaction object is reused and the subscriber count is incremented. The effect is much like joining the original transaction. An Undo-Transaction command rolls back the whole the transaction. To complete the transaction, you must enter a Complete-Transaction command for each subscriber. Because most transactions that are in progress at the same time are related, the default is sufficient for most uses.
If you specify the Independent parameter, this cmdlet creates a new transaction that can be completed or undone without affecting the original transaction. However, because only one transaction can be active at a time, you must complete or roll back the new transaction before resuming work on the original transaction.-RollbackPreferenceSpecifies the conditions under which a transaction is automatically rolled back. The acceptable values for this parameter are:
- Error
The transaction is rolled back automatically if a terminating or non-terminating error occurs.
- TerminatingError
The transaction is rolled back automatically if a terminating error occurs.
- Never
The transaction is never rolled back automatically.
The default value is Error.-TimeoutSpecifies the maximum time, in minutes, that the transaction is active. When the time-out expires, the transaction is automatically rolled back.
By default, there is no time-out for transactions that are started at the command line. When transactions are started by a script, the default time-out is 30 minutes.-ConfirmPrompts you for confirmation before running the cmdlet.-WhatIfShows what would happen if the cmdlet runs. The cmdlet is not run.