# Custom Instrumentation | Sentry for PowerShell

To capture transactions and spans customized to your organization's needs, you must first [set up tracing.](https://docs.sentry.io/platforms/powershell/tracing.md)

To instrument certain regions of your code, you can create transactions to capture them.

```powershell
# Transaction can be started by providing, at minimum, the name and the operation
$transaction = Start-SentryTransaction 'test-transaction-name' 'test-transaction-operation'

# ...
# (Perform the operation represented by the span/transaction)
# ...

$transaction.Finish() # Mark the transaction as finished and send it to Sentry
```

You can add individual spans to a transaction by calling the `StartChild` method on a transaction. This method returns a new `SentrySpan` object that you can use to record the duration of the operation that the span represents. When you're done with the operation, you can call the `Finish` method on the span to mark it as finished.

```powershell
# Transaction can be started by providing, at minimum, the name and the operation
$transaction = Start-SentryTransaction 'test-transaction-name' 'test-transaction-operation'

# Transactions can have child spans (and those spans can have child spans as well)
$span = $transaction.StartChild("test-child-operation")
# ...
# (Perform the operation represented by the span/transaction)
# ...
$span.Finish() # Mark the span as finished

$span = $transaction.StartChild("another-span")
# ...
$span.Finish()


$transaction.Finish() # Mark the transaction as finished and send it to Sentry
```

## [Retrieve a Transaction](https://docs.sentry.io/.md#retrieve-a-transaction)

In cases where you want to attach Spans to an already ongoing Transaction you can use `SentrySdk.GetSpan()`. If there is a running Transaction or Span currently on the scope, this method will return a `SentryTransaction` or `SentrySpan`; otherwise, it returns `$null`.

```powershell
$span = [Sentry.SentrySdk]::GetSpan()

if ($null -eq $span)
{
    $span = Start-SentryTransaction 'task' 'op'
}
else
{
    $span = $span.StartChild('subtask')
}
```
