locked
How to append date/time to each start-transcript session RRS feed

  • Question

  • Hi!  I know I can't be the first person to want to do this, but how can I add the date/time to each powershell transcript?

    Admittedly I'm a newbie...but here's what I'm coming up with so far:

    (Removed actual prompt, and replaced with ">" for this submission) 

          > $dt=get-date -format s
          > $logname="c:\Users\JustaN\Documents\Logged_PS_Cmds-"+$dt+".txt"
          > start-transcript $logname

          start-transcript : Transcription cannot be started.
          At line:1 char:1
          + start-transcript $logname
          + ~~~~~~~~~~~~~~~~~~~~~~~~~
              + CategoryInfo          : InvalidOperation: (:) [Start-Transcript], PSInvalidOperationException
              + FullyQualifiedErrorId : CannotStartTranscription,Microsoft.PowerShell.Commands.StartTranscriptCommand
       
    The variable does work (as evidenced below), but how to get it to work with the start-transcript command?

         > $logname
          c:\Users\JustaN\Documents\Logged_PS_Cmds-2018-11-27T23:20:19.txt

    I was going to stick all this in a .ps1 file once finished.
    I've got to be somewhat close...hints anyone?   Thanks in advance!


    Apologies if this is posted somewhere...I spent an hour or so searching tonight, but was unable to find this exact application anywhere...though like I said, I doubt I'm the first to want to do this!   =)
    • Edited by JustaNumero Wednesday, November 28, 2018 8:13 AM typo & explanation
    Wednesday, November 28, 2018 8:09 AM

All replies

  • The value of $dt contains a colon character which is not valid for filenames. You need format it correctly. 
    This should work:

    $dt=get-date -format yyyy-MM-dd-hhmm

    Also, please use the code formatting tool when posting code. 

    • Proposed as answer by BOfH-666 Wednesday, November 28, 2018 8:28 AM
    Wednesday, November 28, 2018 8:18 AM
  • Read About double quotes.

    My script would look like 

    $dt=get-date -Format "MM-dd-yyyy"
    Start-Transcript -Path "c:\temp\transcript-$dt.txt"

    Kind regards

    Thomas


    regards Thomas Paetzold visit my blog on: http://sus42.wordpress.com

    Wednesday, November 28, 2018 8:51 AM
  • $dt=get-date -format s
    $logname="$env:USERPROFILE\Documents\Logged_PS_Cmds-$dt.txt"
    start-transcript $logname
    


    \_(ツ)_/

    Wednesday, November 28, 2018 9:38 AM
  • Though the Start-Transcript Cmdlet doesn't say anything about it, that still contains an invalid character that prevents the transcription log from actually being created.

    $dt gets formatted as: 2018-11-28T10:53:57

    That cannot be (part of) a filename because of the colon characters in the time notation.

    Wednesday, November 28, 2018 9:56 AM
  • Here is a way around that issue:

    $logname = '{0}\Documents\Logged_PS_Cmds-{1:yyyy-MM-dd HH.mm.ss}.txt' -f $env:USERPROFILE,[datetime]::Now


    \_(ツ)_/

    Wednesday, November 28, 2018 10:12 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee



    Just do it.

    Tuesday, December 4, 2018 8:17 AM