How to use a transcript in PowerShell

Overview

The Start-Transcript cmdlet is used to record the commands, whatever comes after it is called, and the output. We can save the recording to a file.

Syntax

Start-Transcript -Path "path to file"

Parameters

Path: It takes a path as a parameter to save the recording to a file.

Example

#!/usr/bin/pwsh -Command
#starts recording
Start-Transcript -Path "record.txt"
#cmdlet to get files and folders in current directory
Get-ChildItem -Name
#display the recorded transcript
Get-Content "record.txt"

Explanation

In the above code snippet:

  • Line 4: We record the commands and output to a file provided to the path parameter for the Start-Transcript cmdlet.
  • Line 7: We use Get-ChildItem -Name cmdlet to test the cmdlet mentioned above. This will display all the files and folders in the current directory.
  • Line 10: We use Get-Content to display the recorded transcript from the saved file, record.txt.

Free Resources