(8) Definition and use of variables in PowerShell

Source: Internet
Author: User
Tags arithmetic operators

The previous section describes the arithmetic operators in PowerShell, which are used primarily for string connections, composed of bool expressions, and so on to filter the results of command execution in a pipeline, as we will see in subsequent articles.


This section focuses on the definition and use of variables in PowerShell, and what you should be aware of when using variables.


As with variables in all programming languages, variables in PowerShell are also memory units stored in values, and it should be noted that thevariables in PowerShell are single-byte (typically English characters) that begin with a dollar sign ($) , although the ISE supports double-byte characters, However, like any programming language, we do not encourage the use of named variables such as Chinese, such as $a, $b or $my _str.

There are 3 different types of variables in PowerShell .

    • Variables created by the user himself

User-created variables are created and maintained by the user. By default, variables created on the PowerShell command line are valid only in the current PowerShell window. When the window is closed, the variable is automatically destroyed (again, the command window is no longer present and the previously defined variables cannot be referenced again). To save the variable and keep it in effect when the window is closed and opened again, you need to add it to the PowerShell profile (but not recommended). If you are writing a script, you can create a variable with a global or local scope in the script.

    • Automatic variables

Automatic variables, also known as built-in variables, are used to store some of the state information for PowerShell. These variables are created and maintained by PowerShell, and PowerShell changes the value of the variable to maintain its accuracy as needed. Users cannot change the values of these variables. For example, $PSHome variable stores the path to the PowerShell installation directory.

    • Preference variables

The preference variable is the preference setting for the PowerShell Current command window environment, which is created by PowerShell and populated with default values that the user can change the value of, such as $MaximumHistoryCount is the maximum number of entries in the history.

Definition and use of variables

To create (define) a new variable or change the value of a variable, use an assignment statement in the following format

$<variable> = <value>

For example, the following defines a string variable $myStr and modifies its value. If you want to display the value of a variable, enter the name of the variable directly, including the dollar sign $.

PS c:\> $myStr = "This variables defined by the user" PS c:\> $myStrThis variables defined by Userps c:\> $myStr = "User h As changed the value of this variables "PS c:\> $myStruser have changed the value of this variables

You can use single and double quotes when defining and using variables, but you need to be aware of the difference between single and double quotation marks.

PS c:\> $myStr = ' This is a string variables ' ps c:\> $youStr = ' Your want to display the $MYSTR ' PS c:\> $youStryour Wan T to display the $MYSTR

Note that the above variable definition uses single quotation marks, variable $youStr variable $myStr is not replaced by the value it represents, but direct output, meaning that the contents of the single quotation marks are output directly, do not replace the variable value .


PS c:\> $myStr = "This is a string variables" ps c:\> $youStr = "Your want to display the $MYSTR" PS c:\> $youStryour Wan T to display the This is string variables

Note that the definition of the above variable uses double quotation marks, and the variable $myStr in the variable $youstr is replaced by the value it represents, meaning that the variable name in the double quotation marks is replaced with the value represented by the variable .


If you need to output the variable name in a variable in double quotation marks , that is $youStr = "Your want to display the $MYSTR" You do not want to replace the $MYSTR in the variable $youStr, you need to add the English character before the variable name "'" to escape , in the English input state, this key is located under the "ESC" key, note the output of the following command.

PS c:\> $myStr = "This is a string variables" ps c:\> $youStr = "Your want to display the ' $myStr" PS c:\> $youStryour WA NT to display the $MYSTR

Saving of variables

A user-created variable is only available in the current session, and the close session (current command window) variable is lost. If you need to save the variable, you need to save it in the configuration file, and the variables in the configuration file will be loaded automatically when the command window is opened and all sessions will be added.

You can enter $profile at the command line to view the path to the configuration file .

For example, if you need to modify the value of the $VerbosePreference variable to "Continue" (The default is "Silentlycontinue"), you can then enter the command line $profile find the path to the configuration file. Modify it with a text editor such as Notepad, or make changes using Add-content.

The following command adds the new value of the $VerbosePreference variable to the "currentuser,allhosts" configuration file

Add-content-path $profile. Currentuserallhosts-value ' $VerbosePreference = "Continue" '

Variable names that contain special characters

If the variable contains special characters, you can use curly braces {} to force PowerShell to literally interpret the variable name , which is useful if you want to define or reference a variable that contains special characters such as dashes, periods, colons, and parentheses.

PS c:\> ${this is string array}= "a", "B", "C" PS c:\> ${this is string array}abc

The variable name defined by the above command is the ${this is string array}.


If the variable name to be referenced contains parentheses, enclose the variable name in curly braces

PS c:\> Get-childitem ${env:programfiles (x86)} directory: C:mode lastwritetime Length Name---- -----------------------d----2012-8-7 11:40 Documents and SETTINGSD----2016-10 -29 21:35 Dtlfolder ...

If you want to refer to a variable name that contains curly braces, enclose the variable name in curly braces, precede the curly brackets in the variable with the apostrophe "'" character escape brace, and the inverted quotation mark is the key under "ESC" on the keyboard in the citation input state. This is similar to the quoted above if you do not want to replace the variable name with a value in double quotation marks.

PS c:\> ${this ' {value '}is} = 1PS c:\> ${this ' {value '}is}1

Summarize

With regard to variables, it is important to note that variables in PowerShell can use special characters, and variables in PowerShell can store any type of object, Integer, string, array, hash table, service, process, event log, etc. That is, the execution result of any command can be assigned to a variable. Finally, when defining a variable, you cannot define a variable that is the same as the built-in variable name because the built-in variable is read-only and does not allow the user to change what built-in variables are included in PowerShell in the built-in variables of PowerShell.


PS: If you have any questions in your study, please leave a comment in the comments section.

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

(8) Definition and use 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.