#PowershellBasics: The GetType() and GetTypeCode() methods

Leave a comment

October 28, 2019 by Kenneth Fisher

I’m a DBA. Data types are kind of important to me. I mean I know they are important to developers as well but spend some time as a DBA and they get really important. That makes these two methods of particular interest to me.

GetType()

GetTypeCode()

I’m not 100% certain what all of the types available are but it looks like they are the .NET types with the option to create more.

Examples:

#Create/set variables
$IntVar = 1
$IntArrayVar = 1,2,3
$StringVar = "Test"
$StringArrayVar = "Test","MoreTest"

#GetType() Demo
$IntVar.GetType()
$IntArrayVar[0].GetType()
$IntArrayVar.GetType()
$StringVar.GetType()
$StringArrayVar[0].GetType()
$StringArrayVar.GetType()

You’ll notice that arrays are just listed as array and that I had to pull just a single element to get the actual type. This kind of makes sense since these variables aren’t specifically typed. If you do specifically type them you get better information:

#Create/set variables
[int]$IntVarTyped = 1
[int[]]$IntArrayVarTyped = 1,2,3
[string]$StringVarTyped = "Test"
[string[]]$StringArrayVarTyped = "Test","MoreTest"

#GetType() Demo with typed variables
$IntVarTyped.GetType()
$IntArrayVarTyped.GetType()
$StringVarTyped.GetType()
$StringArrayVarTyped.GetType()

You’ll notice that now we get a basetype of System.Array and a name of Int32[] or String[] depending (The []s tell you its an array.)

GetTypeCode() gives you less information (just the name) but is pretty similar.

#GetTypeCode() Demo
$IntVar.GetTypeCode()
$IntArrayVar.GetTypeCode()
$StringVar.GetTypeCode()
$StringArrayVar.GetTypeCode()

Now wait a minute. I only have 4 variables. How did I get back 7 responses? Well GetTypeCode() seems to return a separate line for each element in the array. So an untyped array with two INTs and a STRING would give back explicit information about what’s in the array. INT, INT, STRING for example. So what happens if we type the variables?

#GetTypeCode() Demo with typed variables
$IntVarTyped.GetTypeCode()
$IntArrayVarTyped.GetTypeCode()
$StringVarTyped.GetTypeCode()
$StringArrayVarTyped.GetTypeCode()

Exactly the same. Well that was good to know, if a bit boring.

Note: It appears that while GetTypeCode() is simpler returns a bit more specific information it isn’t always available. I tried this:

$Procs = Get-Process

And GetType() worked while GetTypeCode wasn’t available. From what I can tell GetType() is part of the system object and GetTypeCode is part of the type object.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 3,087 other followers

Follow me on Twitter

ToadWorld Pro of the Month November 2013
%d bloggers like this: