Skip to content

Add -IncludeTimeStamp and -TimeStampFormat parameters to Start-Transcript - #28230

Open
soroshsabz wants to merge 10 commits into
PowerShell:masterfrom
soroshsabz:feature/add-timestamp-to-transcript
Open

soroshsabz wants to merge 10 commits into
PowerShell:masterfrom
soroshsabz:feature/add-timestamp-to-transcript

Conversation

@soroshsabz

@soroshsabz soroshsabz commented Oct 19, 2026 •

Copy link
Copy Markdown

PR Summary

Add opt-in -IncludeTimeStamp and -TimeStampFormat parameters to Start-Transcript that prefix each recorded line of command input and output with a timestamp, so long-running or headless scripts can be reviewed chronologically without cross-referencing external logs.

Start-Transcript ./log.txt -IncludeTimeStamp
Write-Host "This is a test"
Start-Sleep -Seconds 180
Write-Host "The end"
Stop-Transcript

Get-Content log.txt
...
2026-10-19 11:04:52 This is a test
2026-10-19 11:07:52 The end

The timestamp format defaults to yyyy-MM-dd HH:mm:ss (invariant culture, local time) and can be customized with any .NET date and time format string — both standard (e.g. o, s, R) and custom:

Start-Transcript ./log.txt -TimeStampFormat "ddd HH:mm:ss.fff"
Write-Host "This is a test"
Stop-Transcript

Get-Content log.txt
...
Wed 19:08:44.751 This is a test

Specifying -TimeStampFormat implies -IncludeTimeStamp. Invalid format strings (including formats that would render multi-line output and corrupt the transcript) fail fast with a terminating InvalidTimeStampFormat error before any file is created.

PR Context

This PR fixes #24135

Additional design notes:

  • The timestamp is applied when output is queued to the transcript buffer rather than when it is flushed to disk, so it reflects the time the output was actually produced.
  • Multi-line entries (e.g. error records, native command output) have each non-empty line prefixed, as are echoed command lines (PS> ... / >> ...).
  • The transcript header/footer and -IncludeInvocationHeader banners are left unchanged since they already embed timestamps.
  • The feature is opt-in; without the switches, transcript output is byte-identical to before. This keeps existing ingestion mechanisms (e.g. JEA transcript collectors) unaffected, as discussed in the issue.
  • Known limitation: timestamps reflect the time a line was recorded to the transcript. Standalone native command output on Windows is captured from the console screen buffer once the process exits (the console buffer does not track per-line write times), so all lines of such a command receive the command's completion time. Piping native command output into a PowerShell command (e.g. native.exe | Out-Host) streams it through transcription line by line, so each line gets an accurate timestamp; redirecting native output directly to a file (e.g. native.exe > out.txt) bypasses transcription entirely.

PR Checklist

Add an opt-in IncludeTimeStamp switch to Start-Transcript that
prefixes each transcribed line of command input and output with
the time it was recorded, formatted as 'yyyy-MM-dd HH:mm:ss'
using the invariant culture and local time.

The timestamp is applied when output is queued to the transcript
(in TranscriptionCommand/TranscribeResult via the new
TranscriptionOption.AddOutput helper), so it reflects the time
the output was produced rather than the time it was flushed to
disk. Multi-line entries have each non-empty line prefixed.
Transcript header/footer and IncludeInvocationHeader banners are
left unchanged since they already contain timestamps.

Without the switch, transcript output is byte-identical to
before.

Resolves PowerShell#24135
@soroshsabz
soroshsabz requested a review from a team as a code owner October 19, 2026 05:42
Copilot AI balanced review requested due to automatic review settings October 19, 2026 05:42
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Screen-scraped Windows native output receives completion-time timestamps rather than individual line-production times.

1 open issue
What changed in this PR

Adds opt-in transcript timestamps to support chronological log review, addressing #24135.

Changes:

  • Exposes Start-Transcript -IncludeTimeStamp.
  • Prefixes non-empty command and output lines when queued, leaving headers unchanged.
  • Adds tests for timestamped output and default behavior.
File Description
test/​powershell/​Modules/​Microsoft.Powershell.Host/​Start-Transcript.Tests.ps1 Tests timestamp prefixes, multiline output, and default behavior.
src/​System.Management.Automation/​engine/​hostifaces/​MshHostUserInterface.cs Stores the option and timestamps queued transcript content.
src/​Microsoft.PowerShell.ConsoleHost/​host/​msh/​StartTranscriptCmdlet.cs Exposes and forwards the new switch.

Review effort: Balanced

Standalone native command output on Windows is screen-scraped from
the console buffer only after the process exits, so with
-IncludeTimeStamp every line of that block receives the completion
time of the command. The console buffer does not record per-line
write times, so per-line production times cannot be recovered for
this path without redirecting the native output, which would change
how the command renders to the console. Piped or redirected native
output already streams through transcription line by line and gets
accurate per-line timestamps.

Document this in the Start-Transcript parameter help and at the
screen-scrape transcription site, and add a regression test for
timestamped native command output (piped, so it does not depend on
a physical console being available in the test environment).
@soroshsabz
soroshsabz requested a balanced review from Copilot October 20, 2026 16:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The redirection guidance incorrectly promises transcript capture for output redirected to a file.

1 open issue
1 resolved since last review

Review effort: Balanced

Redirecting native command output directly to a file (for example
'native.exe > out.txt') transfers the bytes straight to the file and
bypasses transcription entirely, so it must not be suggested as a way
to capture per-line output. Only piping the native output into a
PowerShell command (for example 'native.exe | Out-Host') streams it
through the pipeline, where each line is recorded and timestamped as
it is produced.

Fix the guidance in the IncludeTimeStamp parameter help, the
screen-scrape comment in NativeCommandProcessor, and the PR
description accordingly.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No unresolved blocking issues remain; timestamping is opt-in, with focused tests and documented limitations.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Add a TimeStampFormat parameter that accepts a .NET custom date and
time format string (for example 'HH:mm:ss' or 'ddd yyyy-MM-dd
HH:mm:ss.fff') for the timestamp prefix applied by -IncludeTimeStamp.
The default format remains 'yyyy-MM-dd HH:mm:ss', rendered with the
invariant culture.

Specifying -TimeStampFormat implies -IncludeTimeStamp, so the switch
does not need to be supplied as well. The format is validated up front
by rendering a sample timestamp, so invalid format strings (or formats
that would render multi-line output and corrupt the transcript) fail
fast with a terminating InvalidTimeStampFormat error before any file
is created.
DateTime.ToString already accepts both standard format strings (such
as 'o', 's' and 'R') and custom format strings, and the sample-render
validation accepts both, so standard format strings already work with
-TimeStampFormat. Update the parameter documentation and the
InvalidTimeStampFormat error message to reference both the standard
and custom format string documentation, and add tests for a standard
format string ('s') and for an unknown single-character specifier
('q'), which is rejected.
@soroshsabz soroshsabz changed the title Start-Transcript: Add -IncludeTimeStamp parameter Add -IncludeTimeStamp and -TimeStampFormat parameters to Start-Transcript Oct 21, 2026
@microsoft-github-policy-service microsoft-github-policy-service Bot added the Review - Needed The PR is being reviewed label Oct 29, 2026
@microsoft-github-policy-service

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@soroshsabz

Copy link
Copy Markdown
Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Commenter does not have sufficient privileges for PR 28230 in repo PowerShell/PowerShell

@soroshsabz

Copy link
Copy Markdown
Author

Dongbo Wang (@daxian-dbw) could you please review this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a timestamp to transcript

2 participants