PowerShell for Windows Admins

Aug 9 2019   4:24AM GMT

Test if a transcript is running

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Powershell

PowerShell has the ability to create a transcript of the commands you run at the console and the results displayed in the console. But how can you test if a transcript is running?

It used to be that you could only have a single transcript running but Windows PowerShell v5.1 and PowerShell v6.x and later allow multiple transcripts to be running in the same session.

Only way I can think of testing if transcript has been started is to use

Get-History | where CommandLine -like ‘Start-Transcript*’

that doesn’t tell if its still running for which you need

Get-History | where CommandLine -like ‘Stop-Transcript*’

PS> (Get-History | where CommandLine -like ‘Start-Transcript*’).Count – (Get-History | where CommandLine -like ‘Stop-Transcript*’).Count

should give a result of zero if no transcripts are running. A positive result indicates transcripts are running. A negative result indicates problems.

The test can be wrapped in a function

function test-runningtranscript {
$starts = (Get-History | Where-Object CommandLine -like ‘Start-Transcript*’).Count
$stops = (Get-History | Where-Object CommandLine -like ‘Stop-Transcript*’).Count

$trans = $starts – $stops

switch ($trans){
0 {$false}
{$_ -gt 0} {$true}
{$_ -lt 0} {Throw “Error!!! Can’t have negative transcripts”}
}
}

You’ll get True returned if there is a transcript running and False if there isn’t.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: