More on Types -- The Many Ways of Exploring PowerShell Types

Posted
Comments None

Sooner or later you will need to learn to identify and explore different data types. This is an essential skill in writing and debugging your own scripts, and also in reverse-engineering the work of others or built-in Windows functionality. I’m not going to try to give a precise definition of what a data type is so here’s Wikipedia if this is new to you. The takeaway is that if an object or function needs a particular type of data, we need to know what that type is and how to satisfy its requirements.

To begin exploring types, let’s use a CMDlet you likely already know and love. In the console, go ahead and execute

Get-Date

You’ll see the date and time in the default format, something like “Tuesday, January 10, 2017 5:00:00 PM.” While it might appear Get-Date is returning the string above, it is actually returning a complex data type with built-in functionality we can leverage to get results really fast. Let’s do the same thing again, but this time ask for the specific date from above and save it to a variable for reuse.

$date = Get-Date "Tuesday, January 10, 2017 5:00:00 PM"

To verify that it saved the date properly, just enter the variable name and hit enter

$date
Tuesday, January 10, 2017 5:00:00 PM

1. Get-Member

You may be used to using Get-Member to figure out what the property names (members) of an object are, but it also returns another valuable piece of information: the object’s TypeName. If you execute

$date | Get-Member

and scroll to the top of the results, you’ll see the line “TypeName: System.DateTime.” Eureka. Now we know that $date is of the type System.DateTime.

PRO TIP: “System.” can be safely removed from applicable type names. [DateTime]$date is synonymous with [System.DateTime]$Date

2. GetType() Method

We can act directly on the built-in object methods to get the data type as well. If you scroll through the results from the last command, you’ll see this line

Name    MemberType
----    ----------
GetType Method

which tells us there is a method on the $date object called “GetType.” We must use the $data object directly to access this method

$date.GetType()
IsPublic IsSerial Name     BaseType
-------- -------- ----     --------
True     True     DateTime System.ValueType

3. Method Overload Definitions

If you look at the output from Get-Member above, you’ll see one of the other methods is called AddMinutes

Name       MemberType Definition
----       ---------- ----------
AddMinutes Method     datetime AddMinutes(double value)

That last column provides the information we need to call the method properly. First, it lets us know that a “datetime” object will be returned. Not all methods will return the same type as the object itself, or even any object at all, so this is not a reliable way to identify the object. What it does tell us though, is what kind of data type is expected when calling the method. The “double value” in parentheses tells us that it expects a value of type “double” (a double is a number that may contain a decimal).

$date.AddMinutes(1.5)
Tuesday, January 10, 2017 5:01:30 PM

Be precise! By dialing the correct types you can ensure reliable scripts and functions. Next time we’ll look at my favorite new feature in version 5, PowerShell 5 classes.

Author

Comments

There are currently no comments on this article.

Comment

Enter your comment below. Fields marked * are required. You must preview your comment before submitting it.





← Older Newer →