GetType Used in PowerShell

Rohan Timalsina Dec 29, 2021
GetType Used in PowerShell

PowerShell has different data types like integer, string, boolean, DateTime, array, booleans, etc. This tutorial will introduce the GetType method in PowerShell to get the current variable data type.

Use GetType to Get the Data Type of Variable in PowerShell

Consider, we have a variable $a as shown below.

$a = Get-Date

You can use the GetType method to view its data type, as shown below. It displays IsPublic, IsSerial, Name, and BaseType properties of a variable.

$a.GetType()

Output:

The Name indicates the data type of a variable.

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     DateTime                                 System.ValueType

You can use GetType().Name to display the data type only.

$a.GetType().Name

Output:

DateTime

Let’s see another example; we have a variable $b.

$b = 1234
$b.GetType()
$b.GetType().Name

Output:

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Int32                                    System.ValueType
Int32

As you can see, the data type is Int32 this time. Similarly, you will get the different data types according to the variables.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website