Explore PowerShell (vii) PowerShell variable _powershell

Source: Internet
Author: User

Variable

In program design, variable is the most basic concept, it is the code that we represent the data at the time of storage. In PowerShell, variables are often used in scripts that can be numbers, characters, strings, or even objects. Of course, like any shell, it also has its own specific variables, such as: $_, $Args, $Error, $Home, $PSHome, and so on.

In PowerShell, all the variables begin with "$" and use "=" To assign a value to the variable. For example:

$StrUser = "mr″<enter>

Or use the "set-variable" command:

Set-variable-name struser-value "Marui" <enter>

Note that you do not need to use "$" in front of the variable name when using the "set-variable" command.

Of course, when declaring variables, avoid some special names, called system reserved Words, listed below:

Break | Continue | do | else | ElseIf | Filter | foreach | function | If | In | return | Switch | Until | where | While

Screen output Variable value:

write-output $StrUser <enter>

or enter the variable name directly, such as:

$StrUser <enter>

Run Result:

Character, string

As with programming, you need to figure out the data type when dealing with characters and strings. The following are the common data type descriptions for PowerShell:

Data types used by PowerShell
Int Signed, 32bit
Long Signed, 64bit
Double Double precision 64bit floating-point
Single Single-precision 32bit floating-point
String Unicode encoded strings
Char Unicode encoded characters, 16bit
Byte unsigned characters, 8bit
Decimal Decimal number, 128bit
Array Array
Xml XML Object
Hashtable Hash table
bool True, False

We use several examples to understand the meaning of data types. First look at the example string:

$strA = "Hello" <enter>

$strB = "world!" <enter>

$strC = $strA + $strB <enter>

$strC <enter>

There are other ways to manipulate strings, such as:

Replace

$strA = "hi! world! "<enter>

$strB = $strA-replace "hi!", "Hello" <enter>

$strB <enter>

Reference

Take a look at the following example:

$strA = "Marui" <enter>

"This is $strA." <enter>

' This is $strA. ' <enter>

In PowerShell, the operation of numbers is very simple, do not need too much explanation, only a few examples:

5 + <enter>

$x =200+1 <enter>

$x <enter>

[int] $y = (7 + * 2)/<enter>

$y <enter>

Sometimes, PowerShell is not a good time to automatically specify data types for us, so when writing a script, declare the data type as much as possible for the variable to prevent errors. In mathematical operations, the following actions are available:

+ Add
- Reducing
* By
/ Except
% Take more
= assigning values
++ Add 1 to the variable, equal to +1.
-- Minus 1 for the variable, equivalent to 1.

Of course, parentheses are also unavoidable.

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.