How to use PowerShell's Start-Transcript command

How to use PowerShell's Start-Transcript command

You'll need a means to log any faults or warnings that occur while running PowerShell programmes automatically. You could write your own log function, but the Start-Transcript cmdlet is a simpler choice.

Everything that happens during a session is sent to a log file by the Start-Transcript cmdlet. The commands you type in a PowerShell session, as well as any output that occurs in the console, are listed here. This can be used in a regular PowerShell session, but it's best for creating log files.

We're going to look at how to utilise the Start-Transcript cmdlet in this tutorial.

In PowerShell, you can use the Start-Transcript command.

In PowerShell, the Start-Transcript cmdlet is quite simple to utilise. It just only a few options, and you don't even have to specify a file path by default.

Parameter                           Description     

-Path                                    The transcript (log) file's location

-Append                               Toggle between appending content to an existing file and appending content to a new file.

-Force                                    Read-only files to be overwritten

-NoClobber                            Prevents existing files from being overwritten.

-IncludeInvocationHeader       In the log file, add a timestamp to a command.

-UseMinimalHeader                Instead of using the complete header, use a short header (PS6+).


-OutputDirectory                       Indicates where the output will be saved (filename will be created automatically)

In PowerShell, there are two ways to use the Start-Transcript cmdlet. You have the option of starting the transcript in PowerShell or including it in your script.

The transcript will be saved in the user's documents folder without any parameters. The filename will be produced automatically and will include the device name, random characters, and a timestamp.

"c:\users\name\documents\PowerShell_transcript.DEVICENAME.qp9EOTN2.20220301132612.txt"

When two PowerShell sessions are initiated at the same time, the random characters ensure that the filename is unique.

We can simply use the cmdlet Start-Transcript to begin the transcript. After we've run the scripts we want to run, we can use Stop-Transcript to stop the transcript.


All of the information that you see in the console will be included in the transcript, as well as a very complete header with information about the host that you used:


Output Directory and Path

When you simply use PowerShell on your own PC, the default path is ideal. However, most of the time, you'll want to consolidate your log files. We can use the -Path parameter or the -OutputDirectory parameter to accomplish this.

We'll need to supply the whole path, including the file name, with path. When you want to have a single log file for a script and add all of the transcripts into one file, this is beneficial.

"# Append the transcript to a mfastatus-result.log file.

Start-Transcript -Path c:\logfiles\mfastatus-result.log -Append

# Or use -NoClobber to prevent overwriting of existing files

Start-Transcript -Path c:\logfiles\mfastatus-result.log -NoClobber"

The -path option can be used without the add, but keep in mind that the Start-Transcript parameter will overwrite any existing text in the file by default.

The -OutputDirectory argument is another possibility. This manner, we can specify the log file's destination directory and let the cmdlet generate a unique filename.

"Start-Transcript -OutputDirectory c:\temp\logfiles

# Result:

Transcript started, output file is c:\temp\logfiles\PowerShell_transcript.WIN11-LAB02.uftVAXsv.20220301045218.txt"

Header with the bare minimum

Each transcript begins with a comprehensive header that includes information on the host. This information is a little too much when used for logging of programmes that run automatically. For this, we can utilise the -UseMinimalHeader argument in PowerShell 6 and higher.

The header information will be limited to a timestamp of when the transcript began:

"Start-Transcript -OutputDirectory c:\temp\logfiles -UseMinimalDeader

# Start of transcript with minimalheader

**********************

PowerShell transcript start

Start time: 20220301135543

**********************"

Incorporate Invocation Header

Before each step or command in the transcript, the invocation header includes a timestamp. This is especially handy if your script is huge and takes a long time to run.

"Start-Transcript -OutputDirectory c:\temp\logfiles -IncludeInvocationHeader

**********************

Transcript started, output file is c:\temp\logfiles\PowerShell_transcript.WIN11-LAB02.VKwZ2RF5.20220301050111.txt

**********************

Command start time: 20220301050122 

**********************

PS C:\> .\Temp\Test-SMTP.ps1"

**********************
Transcript started, output file is c:\temp\logfiles\PowerShell_transcript.WIN11-LAB02.VKwZ2RF5.20220301050111.txt
**********************
Command start time: 20220301050122 
**********************
PS C:\> .\Temp\Test-SMTP.ps1

In a PowerShell script, use Start-Transcript.

You can start the transcript directly from the PowerShell terminal, but you can also add the cmdlet in your script. Simply use the Start-Transcript and Stop-Transcript commands to begin and finish your PowerShell script:

"Start-Transcript -OutputDirectory c:\temp\Logfiles -UseMinimalHeader

# Your script here

Stop-Transcript"

Final Thoughts

When troubleshooting new scripts that run automatically, the Start-Transcript cmdlet comes in handy. Personally, I don't use it very much; instead, for better error handling, I prefer a log function mixed with Try-Catch blocks, such as inside a script.

Keep in mind that you may use the Get-History cmdlet in PowerShell to check your command history if you don't want to use the transcript. This will display you all of the commands you've typed in during your PowerShell session.

If you have any queries, please leave them in the comments section below.