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


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.



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.


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

#GetType() Demo

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

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

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

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.

One thought on “#PowershellBasics: The GetType() and GetTypeCode() methods

  1. […] mentioned arrays in a previous post so I figured I should talk about what they are and how to work with them. Now, I’m going to […]

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 )

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,755 other subscribers

Follow me on Twitter

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