PowerShell to get the variable data type using GetType _powershell

Source: Internet
Author: User
Tags true true

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.