Pages

11/17/2015

PowerShell Get-Date vs. [DateTime]

 

The Cincinnati PowerShell User Group (@CincyPowershell) opens each meeting with a review of a cmdlet. Tonight is was Get-Date's turn. Many of the examples were done with both Get-Date and [datetime], with one example showing that only [datetime] supports the IsLeapYear() method. This got me to thinking… (sometimes a painful thing…) and the light bulb lit up over my head. Get-Date returns an instance of the System.DateTime class, i.e. an object, while [datetime] is a reference to the class and provides easy access to the class's static methods.

 

System.DateTime

To be precise, [datetime] is really [System.DateTime], but System is the default name space and PowerShell is not case sensitive (most of the time). And to be precise, Get-Date with no options is the same as[System.DateTime]::Now.

What's with the two colons? They are PowerShell's way to call a static method on a class. So going back to the IsLeapYear() question, it is a static method of the DateTime class. To access it we write [System.DateTime]::IsLeapYear(2016).

 

Get-Date

Get-Date is a cmdlet with many options, but the default option is to return the current date and time. I.e. [System.DateTime]::Now.

The class that an object was created from is also called it's type. So what's the "type" of a date?

image

 

Instances / Objects

If Get-Date returns an instance, how could I do that with [DateTime]? Two ways, use one of the static methods like .Now or .Today, or create a new object using the New-Object cmdlet.

Using the static methods:

$d = [System.DateTime]::Now    (or just [DateTime].Now)

$d = [System.DateTime]::Today  (or just [DateTime].Today)

Using New-Object:

$d = New-Object System.DateTime    (or just DateTime)

The unique aspect of using New-Object with DateTime is that you get back an object with default values for year, month, day, etc. and not today's date. The actual result is "Monday, January 01, 0001 12:00:00 AM".

image

If you want to create a particular date or time, use one of the class's constructors. (See the DateTime Structure link at the end of the article for details.)

image

There's a nice write up about classes, instances, statics here from Don Jones.

 

And just for fun… and only in PowerShell 5.0

Who needs [System.DateTime]?  Just call the static methods off of any DateTime object! The following all produce the same result, keeping in mind that Get-Date adds the additional overhead of retrieving the current date, and then ignoring it.

(Get-Date)::IsLeapYear(2016)

$d = Get-Date
$d::IsLeapYear(2016)

$d = [System.DateTime]::Now
$d::IsLeapYear(2016)

 

What about PowerShell 3.0?

The examples for 5.0 do not work with 3.0. We have to ask for the "class", i.e. it's Type.

(Get-Date).GetType()::IsLeapYear(2016)

$d = Get-Date
$d.GetType()::IsLeapYear(2016)

$d = [System.DateTime]::Now
$d.GetType()::IsLeapYear(2016)

 

What about PowerShell 4.0?

Beats me! (Don't have a copy in front of me right now.)

 

Discover the Static Methods and Properties of an Object

Get-Member has a –Static parameter to display the static methods and properties of an object:

Get-Date | Get-Member -Static

image

 

More Fun With Static Methods

Most .Net classes are not exposed in PowerShell as cmdlets.

For example, you might need a new GUID…

[System.Guid]::NewGuid()   will return a new GUID object.

[System.Guid]::NewGuid().toString()   will return a new GUID as a string.

image

How about some Math?

Need the value of PI?

[System.,Math]::PI  (or just [Math]::PI)  It's about 3.14159265358979 according to PowerShell and .Net.

How about the square root of 2?

[Math]::Sqrt(2)  (Let's see… I think it's about 1.4142135623731.)

And of course, the Sine of 45 degrees:

[Math]::sin(0.785398163397448)  (in Radians of course) Oh, about 0.707106781186547.

And doing a little converting from degrees to Radians:

[Math]::sin( [Math]::pi/180*45 )

For more of what the Math class can do, see the Math link at the end of the article. Or, wait for it, use PowerShell:

[Math] | Get-Member  -Static

 

Bottom line… if the C# guys can do it, then so can you!

 

So now you know… 2016 is a leap year!  And at least a dozen different ways!

 

(and a quick 5 minute article took over an hour…)

 

References:

Using Static Classes and Methods
https://technet.microsoft.com/en-us/library/dd347632.aspx

How to Create an Object in PowerShell
http://blogs.msdn.com/b/powershell/archive/2009/03/11/how-to-create-an-object-in-powershell.aspx

DateTime Structure (All of the constructors, methods and properties of DateTime)
https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx

A nice write up on class, object, instance and static by Don Jones:
http://powershell.org/wp/2014/09/08/powershell-v5-class-support/

System.Math Class
https://msdn.microsoft.com/en-us/library/system.math(v=vs.110).aspx

 

.

1 comment:

Unknown said...

Great post! I'm new in Powershell, and this article opened my eyes on many issues in PS/.NET! Many thanks!

Post a Comment

Note to spammers...
Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.