I admit, I used to use (get-date).tostring("yyyyMMddHHssmm") when writing timestamps to log. Using this format has several advantages –
- Can be sorted alphabetically (if you need to combine multiple logs)
- Always has same length
- No AM/PM to mess things up ;)
- An international format, understandable across countries and cultures
Then I discovered that it was not necessary to use the programming style above. Get-Date has a –format, so I could change it to Get-Date –f yyyyMMddHHmmss
This is actually ok, but maybe this is more readable –
Get-Date –f "yyyy-MM-dd HH:mm:ss"
Finally, I just realized, that there is a even simpler way of doing it and it even adds timezone and milli-seconds –
Get-Date –f o
"$(Get-Date –f o) Start logging"