How, if at all, is it possible to add timestamps to each line of an output generated by the & PowerShell operator?
Example:
PS H:\\&
You can just use ForEach-Object cmdlet for it (used % alias in below example)
ping 192.168.1.1 | %{ "{0:HH:mm:ss:fff}: {1}" -f (Get-Date), $_ }
result:
23:41:51:301:
23:41:51:302: Pinging 192.168.1.1 with 32 bytes of data:
23:41:55:255: Request timed out.
23:42:00:266: Request timed out.
23:42:05:254: Request timed out.
23:42:10:253: Request timed out.
23:42:10:261:
23:42:10:263: Ping statistics for 192.168.1.1:
23:42:10:265: Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
Or use format like in mjolinor answer:
ping 192.168.1.1 | %{ "{0:o}: {1}" -f (Get-Date), $_ }
result:
2019-04-23T23:45:40.5816185+02:00:
2019-04-23T23:45:40.5845856+02:00: Pinging 192.168.1.1 with 32 bytes of data:
2019-04-23T23:45:44.2560567+02:00: Request timed out.
2019-04-23T23:45:49.2549104+02:00: Request timed out.
2019-04-23T23:45:54.2547535+02:00: Request timed out.
2019-04-23T23:45:59.2547932+02:00: Request timed out.
2019-04-23T23:45:59.2577788+02:00:
2019-04-23T23:45:59.2607707+02:00: Ping statistics for 192.168.1.1:
2019-04-23T23:45:59.2627647+02:00: Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),