(12) Types of variables in PowerShell

Source: Internet
Author: User

The types of variables in Window PowerShell are different from those in high-level languages such as java,c#, and can be used without displaying the types of variables in the specified PowerShell, where variables in PowerShell have greater flexibility. Variables in PowerShell take the. Net framework type.

By default, when a variable has only one value, the data type of the variable is determined by the value assigned to the variable. For example, the following statement creates a variable of type integer (System.Int32):

PS c:\> $a =3

If you need to determine the. NET Framework type of a variable, you can use the FullName property of the GetType () method (which has no parameters) , for example:

PS c:\> $a =3ps c:\> $a. GetType (). Fullnamesystem.int32

Creating a string variable is consistent with the java,c# high-level language, and you need to enclose the value in quotation marks. For example:

PS c:\> $a = "3" PS c:\> $a. GetType (). fullnamesystem.string

If the first value assigned to a variable is a string, PowerShell treats all operations as String operations (a concatenated string) and converts the new value to a string. For example, the following statement executes the result 34 is a string (which can be obtained by its type to verify):

PS c:\> $a = "3" PS c:\> $a + = 4PS c:\> $a 34

You can also enforce the type of a variable in PowerShell.

You can convert a new scalar variable to any type supported by the. NET Framework by placing the type name before the variable name or the square brackets in front of the first assigned value. When you convert a variable, you can determine the type of data that the variable can store. It is also possible to determine that only variables of a conforming type can be assigned to the specified variable. You can also determine how variables behave when a variable is filled.

For example, the following command converts a variable to a string type.

PS c:\> [string] $a =30ps c:\> $a + = 5PS c:\> $a 305

The following statement transforms the first value, not the conversion variable:

PS c:\> $a = [string]30

When you convert a variable to a specific type, the usual convention is to convert the variable instead of the value. However, if the value of an existing variable cannot be converted to a new data type, you cannot re-convert the variable's data type. To change the data type, you must replace the variable's value (the equivalent of creating a new variable), for example:

PS c:\> $b = "string" PS c:\> [int] $b the value "string" cannot be converted to type "System.Int32". Error: "The input string is not properly formatted. "Location line: 1 characters: 8+ [int] $b <<<< + categoryinfo:notspecified: (:) [], runtimeexception + FULLYQUALIFIEDERRORID:RUNTIMEEXCEPTIONPS c:\> [int] $b = 4

placing a data type before a variable name locks the type of the variable unless it is explicitly overridden by specifying another data type.

If you try to give a value that is incompatible with an existing type and you do not explicitly overwrite the type, Windows PowerShell displays an error, such as:

PS c:\> $a = 3PS c:\> $a = "string" PS c:\> [int] $a = 3PS c:\> $a = "string" Cannot convert the value "string" to Type "System.Int32". Error: "The input string is not properly formatted. "Location line: 1 characters: $a <<<< =" string "+ Categoryinfo:metadataerror: (:) [], argumenttransformationmetadataexception + Fullyqualifiederrorid:runtimeexceptionps C:\> [string] $a = "string"

In PowerShell, the data type of a variable that contains multiple items through an array is treated differently from the data type of a variable that contains a single item. Unless you explicitly specify a data type to an array variable, its data type is always System.Object []. This data type is array-specific. You can also override the default type by specifying another type. For example, the following statement converts a variable to a string [] array type:

PS c:\> [string] $a = "string" PS c:\> [string[]] $a = "one", "one", "three"

The PowerShell variable can be any. NET Framework data type. In addition, you can give any fully qualified. NET Framework data types that are available in the current process. For example, the following statement specifies the System.DateTime data type:

PS c:\> [System.DateTime] $a = "4/1/2017" PS c:\> $a April 1, 2017 Saturday 00:00:00

Assigning values to multiple variables

In PowerShell, you can assign values to multiple variables using a single command. The first element of the assignment assigns the first variable, the second element to the second variable, the third element to the third variable, and so on. For example, the following command assigns the value 4 to the $a variable, assigns the value 5 to the $b variable, and assigns the value 6 to the $c variable:

PS c:\> $a, $b, $c = 4,5,6ps c:\> $a 4PS c:\> $b 5PS c:\> $c 6

If the assigned value contains more elements than the number of variables, all remaining values are assigned to the last variable. For example, the following statement contains three variables and five values:

PS c:\> $a, $b, $c = 4,5,6,7,8ps c:\> $a 4PS c:\> $b 5PS c:\> $c 678PS c:\> $c. GetType (). fullnamesystem.object[]

Note that the last variable is of type system.object[].

To assign a value in $c variable to another three variables, use the following format:

PS c:\> $d, $e, $f = $cPS c:\> $d 6PS c:\> $e 7PS c:\> $f 8

The above command assigns the value 6 to the $d variable, assigns the value 7 to the $e variable, and assigns the value 8 to the $f variable.

By concatenating variables, you can also assign a single value to multiple variables. For example, the following statement assigns a value of "string" to all four variables:

PS c:\> $a = $b = $c = $d = 9PS c:\> $a 9PS c:\> $b 9PS c:\> $c 9PS c:\> $d 9

Summarize

This section describes the types of variables in PowerShell, which you should learn by following this section

    1. Depending on the value of the variable, the type of the variable can be judged by the FullName property of the GetType () method of the variable.

    2. PowerShell variable types have different operations, such as the addition of numeric values or the connection of strings.

    3. Assign values to multiple variables or assign a value to multiple variables (concatenation of variables).



This article from "Flower Blossom Fall" blog, declined reprint!

(12) Types of variables in PowerShell

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.