2

I am trying to make PowerShell automatically prepend a timestamp to every line subsequent to invoking my custom ShowTimeStamps function. The function attempts to redirect output thru a filter that prepends the timestamp.

function ShowTimeStamps { $originalOutput = $host.UI.RawUI.WriteLine $host.UI.RawUI.WriteLine = { $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fffffff" Write-Host "$timestamp $_" } } 

The above function is accepted by Powershell, but cannot be successfully invoked. It produces the error:

The property 'WriteLine' cannot be found on this object. Verify that the property exists and can be set. At line:3 char:5 + $host.UI.RawUI.WriteLine = { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyAssignmentException 
1
  • seems like you're trying to override the builtin function prompt. assigning a value to WriteLine (a method) is not valid in this case Commented Oct 13, 2023 at 16:10

2 Answers 2

1

It sounds like you want to prefix every output line with a timestamp (Santiago's answer shows you how add a timestamp to the prompt line).

While overriding the Out-Default cmdlet in regular user code is normally to be avoided, here you can use a proxy function that combines Out-String -Stream with Out-Host to do what you want:

function Out-Default { [CmdletBinding()] param( [Parameter(ValueFromPipeline)] $InputObject ) begin { $scriptCmd = { Out-String -Stream | ForEach-Object { (Get-Date -Format 'yyyy-MM-dd HH:mm:ss.fffffff ') + $_ | Out-Host } } $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin) $steppablePipeline.Begin($PSCmdlet) } process { $steppablePipeline.Process($InputObject) } end { $steppablePipeline.End() } } 
  • Once this function is defined (and in scope), every line in to-host output will be prefixed with a timestamp; e.g.:

     PS> Get-Date 2023-10-13 13:31:57.2870780 2023-10-13 13:31:57.2872810 Friday, October 13, 2023 1:31:57 PM 2023-10-13 13:31:57.2873870 
  • Captured or redirected output will not be affected (however, on trying to display captured / redirected output, timestamps will again surface).

    • However, the timestamps are captured in session transcripts created with Start-Transcript.
  • To revert to the default display behavior, simply remove the function:

     Remove-Item Function:Out-Default 
  • For more information about proxy functions, see this answer.

Sign up to request clarification or add additional context in comments.

Comments

0

It's not clear what exactly you're looking for, it seems you may be looking to override your built-in prompt function. Overriding it to display the date in your desired format is quite easy, as an example:

function prompt { "PS $($ExecutionContext.SessionState.Path.CurrentLocation) - $([datetime]::Now.ToString('yyyy-MM-dd HH:mm:ss.fffffff'))$('>' * ($nestedPromptLevel + 1)) " } 

Then your prompt would show the current location followed by a hyphen followed by the date and time in your desired format:

PS C:\Users\user - 2023-10-13 13:39:58.1856995> 

This is of course as customizable as you want.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.