This post will overview the “GetType()” function in PowerShell.
What is the GetType() Method in PowerShell?
The “GetType()” method checks the data type of the given variable. Using this method, you can find out what type of data is contained in the given variable, along with its name and base type property.
Syntax
In the above-given syntax, we will fetch the value of the “Name” property using the “GetType()” method.
How to Use GetType to Check Variable Type in PowerShell?
In this section, we have compiled some examples to check the variable type using the GetType() method in PowerShell.
Example 1: Check the Data Type of an Integer Variable
This example will examine the integer value’s data type:
$a.GetType().Name
In the above code example:
- First, we have assigned an integer value to a variable “$a”.
- After that, we concatenate the variable with the “GetType()” method:
The output confirms that the given variable is of integer “Int32” type.
Example 2: Check the Data Type of a String Variable
Now, let’s store a string value and perform the same operation:
$a.GetType().Name
The output displayed “String” as the data type of the accessed variable.
Conclusion
The “GetType()” method is used to examine the data type of the variable. To check the data type of any data type, first, the value must be stored in a variable. After that, use the “GetType()” method to get the data type along with its name and the base type. This blog has presented a detailed guide about the GetType() method.