Powershell Transcripts

I am learning Powershell and came across a post somewhere, apologies however I do not recall where I found it, where you can capture a transcript of your Powershell session. A transcript provides the ability to record your Powershell session to a text file. This includes not only all the commands you type but also the output that appears in the Powershell console window. I find this exceedingly useful when trying different things and then a couple of days later trying to remember what I did. Now that I have a transcript I can simply search the transcript files.

I have added the Start-Transcript to my profile and you can too.

In your profile you can put any Powershell commands you like in there to run every time you start a Powershell console. So for example every time I open a Powershell window I would like to navigate to my scripts directory.

$ScriptDir = "D:\Scripts\Powershell"
Set-Location -Path $ScriptDir

For the transcript recording, I use the date so that if I know i did something a couple of days ago I know which file to look in. I use the time as well so that if I start multiple Powershell sessions at once they each get a different transcript file. Multiple Powershell sessions can not share a transcript file and it is unlikely that you would start more than one in a second and if you do well, tuff. 🙂

#If this is the ISE do not start a transcript
if ($Host.Name -eq "ConsoleHost")
{
    #Start a transcript file
    $ISODateTime = Get-Date -uFormat "%Y-%m-%d-%H-%M-%S"
    $TranscriptFile = "$($ScriptDir)\Transcripts\$($ISODateTime).ps1"
    Start-Transcript -path $TranscriptFile -append
} #end if powershell host

I have named my transcript files .ps1 simply so that my text editor syntax highlighting works to make it easier to find cmdlet names and variables. I guess really I should set it to .txt however if you accidentally double click a ps1 it doesn’t run anyway. A great little safety feature built in by the Powershell team.

With these two bits in my Powershell profile I have found this to be invaluable in my learning. I hope it will help you too.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.