|

Using the same exact time with Get-Date

Many times, when using Get-Date in PowerShell, it is very easy to have start and end times for reporting processes.

However, the issue is, the Get-Date cmdlet runs and obtains the time when it is run. If you want to run a specific time, say at exactly midnight every day, there is a way to code PS for that, using the static member assessor option of the DateTime type:

Get-MessageTrace -StartDate ([DateTime]::Today.AddDays(-1)) -EndDate ([DateTime]::Today) | Select MessageID,Received,*Address,*IP,Subject,Status,Size | Export-Csv "$((get-date ([DateTime]::Today.AddDays(-1)) -Format yyyyMMdd)).csv" -NoTypeInformation

This ensures that exactly, the value at midnight for the start and stop times will be used, instead of maybe missing a few seconds or even minutes either way, when running reports or working with scheduled tasks.

Similar Posts

  • |

    cmdlet: Enable-InboxRule

    Q: Is there a way to enable an Inbox rule for a mailbox? A: Yes. You can use the Enable-InboxRule cmdlet to enable an Inbox rule. Inbox rules process messages in an Inbox based on conditions specified and take actions. The enable/disable InboxRule cmdelts, assist in troubleshooting end user’s mailboxes. Enable-InboxRule “Move To Junk Mail” -Mailbox…

  • |

    Exchange links

    Here are some handy short links that help Exchange Server engineers keep up with what’s happening within the Exchange world. As always, it’s best to have some kind of RSS feed notification, using tools such as: Microsoft Flow or IFTTT.com. The Exchange Team blog, you Had Me at EHLO: aka.ms/ehlo Exchange Server build numbers and…

  • MS PowerShell PG upcoming meeting

    Just a reminder, the 3rd Thursday of the month, the Microsoft PowerShell Product Group does a monthly online meeting open to the public! October 15th, 2026, is the next one. https://aka.ms/JoinPSCall and the up-to-date info is always available at https://aka.ms/PSCommunityCall Times are: 12:30pm Eastern 11:30am Central 10:30am Mountain 9:30am Pacific 4:30pm UTC Visual Studio Code…

  • RSS feed

    I’d recommend you setup an RSS feed process with your favorite web site(s). This allows you to simply get a message in your e-mail inbox whenever a site you follow updates their content. I recently added an RSS feed option to this blog site. For WordPress hosted sties, like this one, all you need to…