Going back in time

Did it ever happen to you to spend a decent amount of time working on a piece of code in the console, making it work and then closing the shell thinking to yourself: Oh cool, I found a solution!
5 minutes later you try to recreate what you did and something is not working? You bang your head against the wall wishing that time travel was possible so you could go back in time and take just a sneak peak at what you wrote.
Time travel is not possible. At the moment. But even without it, you can ‘recover’ the code. The Start-Transcript cmdlet is here to help you.
With Start-Transcript you can record whole or a part of a Windows PowerShell session in a text file. The transcript includes all command(s) you have typed and all output that appears in the console. Running Start-Transcript without any parameters starts a transcript in the default file location (MyDocuments folder) with a file name in the format of "PowerShell_transcript.{0:yyyyMMddHHmmss}.txt".
PS > Start-Transcript
Transcript started, output file is C:\Users\UserName\Documents\PowerShell_transcript.20100510162158.txt
We can change the transcript file location with the Path parameter, prevent any existing files from being overwritten
with the NoClobber parameter and even add a transcript to the end of an existing file by specifying the Append switch parameter. For more code examples type: ‘Get-Help Start-Transcript –Examples’ in your console.
It is possible to call Start-Transcript without giving it a file path and give the file name a format of your choice.
When Start-Transcript is executed it looks for a global variable named 'TRANSCRIPT'. If the variable exists, its value is used as the path to the transcript file.
function Save-Transcript{
$global:TRANSCRIPT = "D:\Scripts\Transcripts\PSLOG_{0:dd-MM-yyyy}.txt" -f (Get-Date)
Start-Transcript -Append
}
Save-Transcript
With the function above (which I use in my profile), you get a new transcript file per day. If the file exists, the transcript is appended, otherwise a new file is created. There is nothing to worry about anymore, you can safely close your console session at any time knowing that all your work is saved.
See Also:
Stop-Transcript