DCSIMG
Going back in time - Shay Levy

Shay Levy

If you repeat it, PowerShell it!

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

Comments

Twitter Trackbacks for Going back in time - Shay Levy [microsoft.co.il] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Going back in time - Shay Levy         [microsoft.co.il]        on Topsy.com

# May 11, 2010 11:03 AM

joe said:

or powershell could simply implement a persistant history like the traditional unix shell does!

# May 12, 2010 7:40 AM

ScriptFanatic said:

Joe, put the code in your $profile to make it permanent.

# May 12, 2010 12:25 PM

LJF said:

Ok This is exactly what i need but i get this error:

Start-Transcript : This host does not support transcription.

At C:\Users\MyAccount\Documents\WindowsPowerShell\profile.ps1:20 char:18

+     Start-Transcript <<<<  -Append

   + CategoryInfo          : NotImplemented: (:) [Start-Transcript], PSNotSupportedException

   + FullyQualifiedErrorId : NotSupported,Microsoft.PowerShell.Commands.StartTranscriptCommand

How Can I make it work?

# September 27, 2010 3:53 AM

ScriptFanatic said:

What editor you use? Some script editors does not support this functionality.

# September 27, 2010 9:26 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: