Loading...
X

How to determine an object’s type in PowerShell

Table of contents

1. How to determine an Object's type in PowerShell

2. Determining the object type with .GetType() (as well as .GetType().Name and .GetType().FullName

3. How to get the object type with Get-Member

4. Checking object types with the -is and -isnot type operators

5. How to check the type of collection elements

6. The .GetType().IsArray property to test if an object is an array

Conclusion


1. How to determine an Object's type in PowerShell

There is no special tool (like a cmdlet or property) in PowerShell that shows the object's type. However, some cmdlets do show the object's type as part of their output, and you can also find out the object's type using the .NET API.

The output may vary and be a bit confusing due to the different approaches.

To make matters even more confusing, some objects are collections containing objects of different types – we'll cover this at the very end, including how to iterate through all the items in a collection and determine their type.

I'll show you a few ways to determine an object's type in PowerShell here, and maybe you'll find something that suits your needs.

For the examples in this post, we'll be defining the type of variables with the following contents:

$a = 'pwsh.ru and Suay.site' # String
$b = 456 # Number, Int
$c = @{ Number = 1; Shape = "Square"; Color = "Blue"} # Hash-table
$d = 22,5,10,8,12,9,80,'Suay.site','pwsh.ru' # Array
$e = Get-ChildItem
$f = Get-Date
$g = Get-ChildItem | Get-Member

2. Determining the object type with .GetType() (as well as .GetType().Name and .GetType().FullName

You can determine the object type with its .GetType() method:

$a.GetType()
$b.GetType()
$c.GetType()
$d.GetType()
$e.GetType()
$f.GetType()
$g.GetType()

The .GetType() method provides metadata about the object, such as its name, base type, the assembly it is taken from, etc.

As you can see, the object type is specified in the Name column. But for an array, the type is specified as “Object[]”, which is generally correct, but too general a characteristic. However, in the BaseType column, you can see a mention of the array.

You can limit the command to displaying only the Name field, for this use .GetType().Name. For example:

$a.GetType().Name
$b.GetType().Name
$c.GetType().Name
$d.GetType().Name
$e.GetType().Name
$f.GetType().Name
$g.GetType().Name

Perhaps, in some situations, FullName will suit you better:

$a.GetType().FullName
$b.GetType().FullName
$c.GetType().FullName
$d.GetType().FullName
$e.GetType().FullName
$f.GetType().FullName
$g.GetType().FullName

As you may remember, for arrays, a more precise definition of the data type is contained in the BaseType property, if desired, you can get only the BaseType name by adding it to your object .GetType().BaseType.Name:

$a.GetType().BaseType.Name
$b.GetType().BaseType.Name
$c.GetType().BaseType.Name
$d.GetType().BaseType.Name
$e.GetType().BaseType.Name
$f.GetType().BaseType.Name
$g.GetType().BaseType.Name

Documentation for the Object.GetType method: https://learn.microsoft.com/en-us/dotnet/api/system.object.gettype

3. How to get the object type with Get-Member

Get-Member is primarily intended for getting information about the members (properties and methods) of an object (or static members of a class if you use the -Static parameter). For example:

$a | Get-Member

Notice the first line that starts with “TypeName:”. You can get only the TypeName output with the following construct:

(OBJECT | Get-Member)[0].TypeName

In this command, replace OBJECT with the object whose type you want to know.

Examples:

($a | Get-Member)[0].TypeName
($b | Get-Member)[0].TypeName
($c | Get-Member)[0].TypeName
($d | Get-Member)[0].TypeName
($e | Get-Member)[0].TypeName
($f | Get-Member)[0].TypeName
($g | Get-Member)[0].TypeName

We got a clearer view of some objects (something more than “Object[]”), for example:

  • DateTime
  • DirectoryInfo
  • MemberDefinition

But for the array we got the type “Int32”, which is clearly wrong. Note that if you want information about the members of a collection object, you should use

Get-Member -InputObject $COLLECTION

instead of piping the object

$COLLECTION | Get-Member

because piping will unwrap the collection and you will get the members of the collection elements, not the members of the collection object itself.

So try this:

(Get-Member -InputObject OBJECT)[0].TypeName

In this command, replace OBJECT with the object whose type you want to know.

Examples:

(Get-Member -InputObject $a)[0].TypeName
(Get-Member -InputObject $b)[0].TypeName
(Get-Member -InputObject $c)[0].TypeName
(Get-Member -InputObject $d)[0].TypeName
(Get-Member -InputObject $e)[0].TypeName
(Get-Member -InputObject $f)[0].TypeName
(Get-Member -InputObject $g)[0].TypeName

As you can see, the DateTime data type remains, and DirectoryInfo and MemberDefinition are again designated as Object[]. Arrays are also designated as Object[].

4. Checking object types with the -is and -isnot type operators

-is and -isnot refer to the type operators and the comparison operators.

The -is operator returns TRUE if the input is an instance of the specified .NET type.

The -isnot operator returns TRUE if the input is not an instance of the specified .NET type.

Examples:

$a -is [String]
True

$d -is [Array]
True

$e -is [System.IO.DirectoryInfo]
False

$e[0] -is [System.IO.DirectoryInfo]
True

(get-date) -is [DateTime]
True

(get-date) -isnot [DateTime]
False

5. How to check the type of collection elements

I hope you are not too confused by this point. If not, then we continue 🙂

Some of the objects whose type we want to check are collections. In the commands above, they were often defined as “Object[]” or an array. But these objects can contain collection elements of different types.

To list the elements of a collection and check their types, you can use the following construction:

OBJECT | ForEach-Object {$_.GetType().Name}

In this command, replace OBJECT with the object for which you want to iterate over all the elements and find out the type of each of these elements.

For example:

$a | ForEach-Object {$_.GetType().Name}

The $a variable contains only one element, and it is a string.

The $d variable contains an array, let's check its contents:

$d | ForEach-Object {$_.GetType().Name}

Example output:

Int32
Int32
Int32
Int32
Int32
Int32
Int32
String
String

That is, we can see that some members of the array are numbers, and some are strings.

The $e variable contains the result of executing the Get-ChildItem cmdlet:

$e | ForEach-Object {$_.GetType().Name}

This collection has two types of objects:

  • DirectoryInfo
  • FileInfo

The type operators (-is and -isnot) discussed above always work with the entire input object. That is, if the input object is a collection, the collection type is tested, not the collection element types. This is why the output is different for $e (the entire collection type is tested):

$e -is [System.IO.DirectoryInfo]

And for $e[0] (the first element of the collection is tested):

$e[0] -is [System.IO.DirectoryInfo]

But if you want, you can also test with -is and -isnot on each element of the collection, like this:

$e | ForEach-Object {$_ -is [System.IO.DirectoryInfo]}

6. The .GetType().IsArray property to test if an object is an array

The .GetType() method has a .IsArray property that lets you test if an object is an array:

OBJECT.GetType().IsArray

In this command, replace OBJECT with the object you are testing.

Examples:

$a.GetType().IsArray
False

$b.GetType().IsArray
False

$c.GetType().IsArray
False

$d.GetType().IsArray
True

$e.GetType().IsArray
True

$f.GetType().IsArray
False

$g.GetType().IsArray
True

Of course, there are many other ways to check if an object is an array or another type (string, number, hashtable, etc.), the .GetType().IsArray property is mentioned here because we have talked quite a bit about the .GetType() method in this post. If you are wondering if there are properties like .GetType().IsString, .GetType().IsInt, and others like that, the answer is no – there are no such properties.

Conclusion

So, an object can consist of a single element (be a string, a number, a hash table, a DateTime object, etc.) or be a collection consisting of several elements, often of different types.

Hash tables are considered a single object and the stored values ​​are not iterated over with ForEach-Object.

And an array is considered a collection, the elements of which can be iterated over with ForEach-Object.

The output of a cmdlet can be either an array or a special object – for example, the output of Get-Date is a single [DateTime] object.

The output of a cmdlet can also be an array consisting of objects. For example, the output of Get-ChildItem is an array of [System.IO.DirectoryInfo] and [System.IO.FileSystemInfo] objects.


Leave Your Observation

Your email address will not be published. Required fields are marked *