# Help formatting log output

**URL:** <https://forums.powershell.org/t/help-formatting-log-output/15367>\
**Category:** PowerShell Help\
**Created:** [December 10, 2020, 8:30am UTC](https://forums.powershell.org/t/help-formatting-log-output/15367 "2020-12-10T08:30:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [December 10, 2020, 8:30am UTC](https://forums.powershell.org/t/help-formatting-log-output/15367/1 "2020-12-10T08:30:06Z")

</div>

I am trying to find the best method to format my PowerShell logging. What I am trying to achieve is the following. I want to precede all lines sent to the log file with the following:

[07-30-2020 17:15:44 -0700 INFO] yada yada yada …

So, precede log entries with [month-day-year hours:minutes:seconds timezone INFO] similar to Splunk logging.

I have no problems getting this format, but it seems like a performance hit to calculate the date/time information for each line logged (of which there are many). Is there a better way then running Get-Date for each line logged?

I should also point out this is all run through Start-Transcript.

Thanks in advance for any help/suggestions.

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [December 10, 2020, 8:39am UTC](https://forums.powershell.org/t/help-formatting-log-output/15367/2 "2020-12-10T08:39:43Z")

</div>

Have you tried .NET methods? I would imagine that Get-Date is just a wrapper for these. Would have to use ToString() or string format after you have the date.

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [December 10, 2020, 9:06am UTC](https://forums.powershell.org/t/help-formatting-log-output/15367/3 "2020-12-10T09:06:43Z")

</div>

Thanks a million Rob. You have been as usual very helpful 🙂

> **[DateTimeOffset Struct (System)](https://docs.microsoft.com/en-us/dotnet/api/system.datetimeoffset?view=net-5.0)**
>
> Represents a point in time, typically expressed as a date and time of day, relative to Coordinated Universal Time (UTC).

&nbsp;

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [December 10, 2020, 9:25am UTC](https://forums.powershell.org/t/help-formatting-log-output/15367/4 "2020-12-10T09:25:51Z")

</div>

Oops, totally meant to put a link in there:

> **[\#PSTip Working with DateTime objects in PowerShell using \[System.DateTime\]](https://powershellmagazine.com/2012/10/04/pstip-working-with-datetime-objects-in-powershell-using-system-datetime/)**
>
> When working with dates in PowerShell it is often useful to leverage the .NET Framework System.DateTime class. For example to display the current date and time the Now method can be used:

Also, just a quick test shows that Get-Date is actually faster…

```
PS C:\Users\rasim> Measure-Command {
    Get-Date
}

Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 7
Ticks : 76788
TotalDays : 8.8875E-08
TotalHours : 2.133E-06
TotalMinutes : 0.00012798
TotalSeconds : 0.0076788
TotalMilliseconds : 7.6788

PS C:\Users\rasim> Measure-Command {
    [datetime]::Now
}

Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 11
Ticks : 114846
TotalDays : 1.32923611111111E-07
TotalHours : 3.19016666666667E-06
TotalMinutes : 0.00019141
TotalSeconds : 0.0114846
TotalMilliseconds : 11.4846
```

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [December 10, 2020, 9:33am UTC](https://forums.powershell.org/t/help-formatting-log-output/15367/5 "2020-12-10T09:33:48Z")

</div>

Thanks again Rob. I am going with

as it gets me both the Date/Time and the TimeZone in one command and I am including both in my log prefix.

My testing was a tad different then yours, just an FYI

`PS C:&gt; Measure-Command {DateTimeOffset::Now}`

`Days : 0`  
`Hours : 0`  
`Minutes : 0`  
`Seconds : 0`  
`Milliseconds : 0`  
`Ticks : 1087`  
`TotalDays : 1.25810185185185E-09`  
`TotalHours : 3.01944444444444E-08`  
`TotalMinutes : 1.81166666666667E-06`  
`TotalSeconds : 0.0001087`  
`TotalMilliseconds : 0.1087`

&nbsp;

`PS C:&gt; Measure-Command {get-date}`

`Days : 0`  
`Hours : 0`  
`Minutes : 0`  
`Seconds : 0`  
`Milliseconds : 0`  
`Ticks : 2985`  
`TotalDays : 3.45486111111111E-09`  
`TotalHours : 8.29166666666667E-08`  
`TotalMinutes : 4.975E-06`  
`TotalSeconds : 0.0002985`  
`TotalMilliseconds : 0.2985`

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/help-formatting-log-output/15367/6 "2024-05-16T20:28:10Z")

</div>

Closed on May 16, 2024, 8:28pm UTC


