Q. How can I easily add a timestamp to any text output in PowerShell?
A. Filters are a useful capability that can manipulate incoming objects and is really just a function that only has a process scriptblock and it runs once for each incoming object making it useful when evaluating large amounts of data. For example below is an example filter that simply adds a timestamp to any text to sent to it:
filter timestamp {"$(Get-Date -Format G): $_"}
I can then send output to the filter via the pipeline, for example:
PS C:\> write-output "hello world" | timestamp 8/10/2016 3:45:22 PM: hello world
Note that the time was added to the start of the text.
0 comments
Hide comments