Derek Schauland shows how the transcript function in PowerShell can help you work better.
PowerShell has some really slick options when it comes to writing code at the command line. Some of these features came over from the DOS command prompt, like being able to arrow up to see the last command (yes I know, Linux has this too). In this post, I want to talk about using the transcript file in PowerShell.
What is a PowerShell transcript?
A transcript is a file that contains all of the command line entries entered while a session is being recorded. Transcripts can be turned on or off at any time but can also come in really handy when working on sizable projects. When turned on, a transcript file is created with a date and timestamp in the filename to ensure uniqueness.
How do I turn it on?
To enable a transcript for your current session, simply enter start-transcript. This will record all of the commands that follow until the stop-transcript is run or the session is closed.
Start-transcript supports the following parameters:
- -path - this allows you to change the location of the current transcript file
- -append - add to an existing transcript file rather than starting a new one
- -force - can append data to an existing read-only file
- -noclobber - will not overwrite the contents of an existing file
- -confirm - prompt for confirmation before executing
- -whatif - presents results of the command without performing them
- [commonparameters] - these are common parameters supported by many cmdlets including erroraction, outbuffer, verbose, debug, errorvariable, outvariable, warningaction, warningvariable
Why use transcripts?
When I first saw the transcript cmdlets I wasn't sure how I would use a feature that recorded what I was typing. Then I started testing items to create a function.
I was able to test build the function at the command line and be sure all of the commands were correctly written as I put them together and execute the function without having to worry about the order of operations or retyping the whole thing into a text file. I could simply copy and paste the function from the transcript text file into the script. This completely took away the need to reinvent the wheel, at least for small functions.
Another thing I find useful about transcripts in PowerShell is that the buffer cache for commands is set to a default of about 60 lines. If I am spending a good deal of time working in the shell and testing things, I will be past 60 lines in a hurry, which means what I was working on an hour ago might not be sitting in the cache when I want to reuse it. If I begin with start-transcript, all of the lines are written to the transcript file and can be reused.
Remember... transcripts can save you work
To turn on transcripts, simply enter start-transcript at the command line in PowerShell or add the cmdlet to your profile to have it enabled whenever you start a PowerShell session.
To turn off the feature enter stop-transcript or close the PowerShell window. Hopefully, this simple but powerful feature will help you make better use of PowerShell.