Using Start-Transcript in ISE Profile

This week on the Scripting Guys blog they have been doing some posts on PowerShell user profiles and the kinds of things people have in them and what they are for. This prompted me to finally do something I have been meaning to for a while: Add something to my ISE Profile that starts a transcript every time I open ISE.

Some time ago I stumbled upon this module that lets you run Start-Transcript in ISE. I have only used it sparingly however, because I always forget to turn it on. Not after today!

The format for Start-Transcript is pretty simple, all you need to have is a path to store the transcript file in.

Start-Transcript -Path C:\Scripts\Transcript.txt

Now, this is great and all, but when I open up my ISE Console tomorrow, or the next day, I don’t want it to just keep piling everything into the same text file. I wonder if I can just name the file with today’s date using the Get-DateTime cmdlet?

PS C:\Scripts> Get-Date

Wednesday, May 21, 2014 8:32:28 AM

Well, that’s not real helpful. I spent about the next 10 minutes exploring all the various Methods and Properties of Get-Date, and wasn’t having any luck so I decided to go to the help file. It also just so happens that the last example is exactly what I am looking to do, so I included that as well.

Get-Help Get-Date -Examples

    -------------------------- EXAMPLE 10 --------------------------
    
    The first command uses the Format parameter with a value of "o" to generate a timestamp string.
    PS C:\>Get-Date -Format o
    2012-03-08T10:55:55.6083839-08:00
    
    The second command prepares the timestamp to be used in a directory name. The command replaces the colon characters (:) in the string with dots (.) and saves the result in the $timestamp 
    variable. Replacing the colons prevents the characters that precede each colon from being interpreted as a drive name.
    PS C:\>$timestamp = Get-Date -Format o | foreach {$_ -replace ":", "."}
    
    The third command uses the Mkdir function to create a directory with the name in the $timestamp variable.
    PS C:\>mkdir C:\ps-test\$timestamp
        Directory: C:\ps-test
    
    Mode                LastWriteTime     Length Name
    ----                -------------     ------ ----
    d----          3/8/2012  11:01 AM            2012-03-08T11.00.24.4192623-08.00
    
    
    This example shows how to use the Get-Date cmdlet to create a timestamp and how to use the timestamp in or as part of a directory name.

I don’t like the format of that DateTime object, so I decided to investigate what other types of objects there were.

Get-Help Get-Date -ShowWindow

I searched for Format, which right in the help file gave me the link to the MSDN article explaining the different format options. Looking at all the different format patterns, I elected to just start at the type with pattern d, and go from there.

PS C:\Scripts> Get-Date -Format d
5/21/2014

Well look at that! Exactly what I need, except for the /’s because I can’t use those in a filename. Referencing the example in the help file, I change my command to this.

PS C:\Scripts> Get-Date -Format d | foreach {$_ -replace "/","."}
5.21.2014

Perfect! Now, let’s tie this all together.

PS C:\Scripts> Start-Transcript -Path C:\Scripts\$date.txt -Append
Transcript started, output file is C:\Scripts\5.21.2014.txt

I added the Append parameter because if I open close ISE throughout the day I want it to just keep adding on to the existing file and not overwrite the existing one every time I restart ISE during a given day.

All you need to do to add this to your profile is download the module linked at the beginning of the post, and then add these 3 lines to your profile (you can use notepad $profile from PowerShell to make it easy on yourself).

Import-Module Transcript

$date = Get-Date -Format d | foreach {$_ -replace "/","."}
Start-Transcript -Path C:\Scripts\$date.txt -Append
  • why not do all of the formatting at one time:

    get-date -format yyyy.MM.dd

    • Jacob

      Because I had no idea I could do that :). I tried to do that using Get-Date -Format dd,mm,yyyy and that failed for the simple reason I didn’t put it in quotes (which I know now was the problem). I never even tried to do it your way (with the periods as a separator), which is a much better way to do it :).

      Thanks for the comment!

  • Scott Saari

    Hi, Jacob,

    I always enjoy your posts. This is how I do this, maybe a bit simpler:
    # How to format a file name containing the date and time:
    $now = Get-Date
    $timestamp = “{0:yyyy-MM-dd_HHmmss}.txt” -f $now

    If you put it at the start of the file name, it makes it sortable by name or date.