header image

Start and end dates

Posted by: | October 31, 2016 Comments Off on Start and end dates |

Be careful with start and end dates when search for files or through event logs.  Often people want to see what happened yesterday

If you do this

PS> $end =  Get-Date
PS> $start = (Get-Date).AddDays(-1)
PS> $start

30 October 2016 16:32:35

PS> $end

31 October 2016 16:32:16

And you get the last 24 hours

 

if you really want just yesterday you need to create the exact dates and times

PS> $start = Get-Date -Day 30 -Month 10 -Year 2016 -Hour 00 -Minute 00 -Second 00
PS> $end = Get-Date -Day 31 -Month 10 -Year 2016 -Hour 00 -Minute 00 -Second 00
PS> $start

30 October 2016 00:00:00

PS> $end

31 October 2016 00:00:00

 

I know there are easier ways but using Get-Date shows exactly what’s happening and is simple

under: PowerShell

Comments are closed.

Categories