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.