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?
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]") $_" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With