Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepending a timestamp to each line of output from a command or executable

I want to prepend a timestamp to each line of output from a command. For example:

foo
bar
baz

would become:

[2011-12-13 12:20:38] foo
[2011-12-13 12:21:32] bar
[2011-12-13 12:22:20] baz

where the time being prefixed is the time at which the line was printed. How can I achieve this with PowerShell?

like image 395
runamuk0 Avatar asked Oct 26 '25 12:10

runamuk0


1 Answers

Assuming your command is a external command named foo, you can pipe it's output through a ForEach-Object to add the timestamp:

foo | ForEach-Object { "$(Get-Date -Format "[yyyy-MM-dd HH:mm:ss]") $_" }

like image 66
Burt_Harris Avatar answered Oct 29 '25 06:10

Burt_Harris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!