Power shell Study Notes (3) variables

Source: Internet
Author: User

Power shell provides the definition function for user variables.

Variables do not need to be declared first (of course, they are in uniformity. For some global variables, we recommend that you declare them first and assign values). The variable names are prefixed with $. If "$ A" is called and no value is assigned, the $ a variable is equivalent to undefined.

You do not need to declare the type of a variable. You can directly assign values to the variable. It is similar to the JS script var variable usage. The last Value Type of the variable data type variable prevails.

For example:

$ A = 1 # variable $ A is a value type

$ B = "AA" # The variable $ B is a string

$ B = $ A + 1 # variable $ B programming value type, value: 2

Note: Power shell is case-insensitive (except for strings) # It is a single-line annotator. # The post-signs are all considered comments. Currently, there seems to be no paragraph comments or partial comments.

Rule for defining variables
-The variable can be a number $123
-The variable can be a string $ ABC.
-Variables can be special characters $ {as #}, but special characters must be wrapped in {}.

Write a naive example:

$ A = 1

$ B = 2

$ C = $ A + $ B

Write $ C

The result is as follows:

Note: A variable has a life cycle. If the PS window is closed, all variables will be automatically released. Otherwise, the variable will always exist unless you manually delete the variable.

Of course, you can also use the cmdlet command to operate variables. The command is as follows:

Variable Assignment Method: set-variable varname 100 (this method has declaration effect)
Value Method: Get-variable varname
Clear value: Clear-variable varname
Delete variable remove-variable varname
Obtain multiple variables such as var1 var2 var3: Get-variable var1, var2, var3

Example:

Set-variable a 100

Set-varable B 200

Get-variable

Get-variable a, B

Clear-variable

Get-variable

Remove-variable

Get-variable

Effect:

Note: Why is the last error? What do you mean...

Variables support special value assignment methods. They are actually concatenated as strings, which are very useful.

$ A = 111

$ B = 222

$ C = "$ A, $ B"

Write $ C

Effect:

Note: It is good to splice strings.

Last write a date usage:

$ Date = Get-date # Get the current time

Write $ date

$ Date. adddays (3) # The current time plus three days

Write $ date

Effect:

Note: Of course, adddays can be used, which means that addmonth and addyear can also be used. Yes. What about negative numbers?

Variables are useful techniques. Proper Use of variables can improve the readability of scripts and simplify code and later maintenance.

Related Article

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.