This article describes how to get the data type of a variable in PowerShell, using the GetType () function to accomplish this task.
First look at an example:
Copy Code code as follows:
PS c:\users\zhanghong> $i =1
PS c:\users\zhanghong> $i. GetType ()
IsPublic isserial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
We assigned a number "1" to the variable $i, but we did not force the variable to specify a data type. But we use the GetType () function to look at its type, and as a result we can see that the system automatically defines it as an integer.
The GetType () function is a method for all objects in the. NET framework, and this method returns the type of the object. Of course, the object here is. NET is an object-all variables are objects. The specific usage is:
< variable name. GetType (), the return value is a runtimetype type.
Copy Code code as follows:
PS c:\users\zhanghong> $x = $i. GetType ()
PS c:\users\zhanghong> $x. GetType ()
IsPublic isserial Name BaseType
-------- -------- ---- --------
False True RuntimeType System.Type
We can simply get the data type by using the RuntimeType Name property directly.
Copy Code code as follows:
PS c:\users\zhanghong> $i. GetType (). Name
Int32
About PowerShell using GetType to get variable type, this article on the introduction so much, I hope to help you, thank you!