PowerShell v2: Get-Date vs [System.DateTime]::Now - What's the difference?

PowerShell v2: Get-Date vs [System.DateTime]::Now - What's the difference?

  • Comments 1
  • Likes

I was trying to figure out why people use the more complicated [System.DateTime]::Now instead of the simple Get-Date.

They do the same thing and they both return an object of type "System.DateTime".

PS C:\> Get-Date
Thursday, April 08, 2010 10:46:48 PM

PS C:\> [System.DateTime]::Now
Thursday, April 08, 2010 10:46:59 PM

The only possible different is the performance. Yes, Get-Date is slightly slower, but they both really do the job in less than a millisecond. I had fun testing it, though, creating a a PowerShell one-liner to measure how long they each take to run, with 5,000 samples of each and discarding any result that did not show up at least 10 times out of the 5,000. I didn't even have to launch Excel...

PS C:\> 1..5000 | %{ (((Measure-Command {[System.DateTime]::Now}).TotalMilliseconds).ToString()).Substring(1,3) } | Group | ?{$_.Count -gt 10} | Sort Name | Select Count, Name

                                                      Count Name
                                                      ----- ----
                                                       4888 .03
                                                         43 .04
                                                         15 .05
                                                         26 .07

PS C:\> 1..5000 | %{ (((Measure-Command {Get-Date}).TotalMilliseconds).ToString()).Substring(1,3) } | Group | ?{$_.Count -gt 10} | Sort Name | Select Count, Name

                                                      Count Name
                                                      ----- ----
                                                       2288 .11
                                                       2224 .12
                                                        246 .13
                                                         85 .14
                                                         32 .15
                                                         14 .16
                                                         17 .17

So, unless you care about the difference of doing it in 0.03 milliseconds instead of 0.115 milliseconds, they are pretty much the same. :-)

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • my only pet peeve is why does the property ".date" show the day and time which is different then ".datetime"  I don't think ."date" should show the time at all!