Explore PowerShell (7) PowerShell Variables

Source: Internet
Author: User

Variable

In program design, variables are the most basic concept. They are the codes used to represent data during storage. In PowerShell, variables are usually used in scripts. They can be numbers, characters, strings, and even objects. Of course, like any shell, it also has its own specific variables, such as $ _, $ Args, $ Error, $ Home, and $ PSHome.

In PowerShell, all variables start with "$" and use "=" to assign values to variables. For example:

$StrUser = “MR″ <enter>

Or run the "set-variable" command:

set-variable -name StrUser -value “MaRui” <enter>

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

Of course, some special names should be avoided when declaring variables. These names are called "system reserved words" and are listed as follows:

Break | continue | do | else | elseif | filter | foreach | function | if | in | return | switch | until | where | while

Screen output variable value:

write-output $StrUser <enter>

Or directly enter the variable name, such:

$StrUser <enter>

Running result:

Character, string

Similar to program design, data types must be clarified when processing characters and strings. The following describes common PowerShell data types:

Data types used by PowerShell
Int Signed, 32bit
Long Signed, 64bit
Double Double Precision 64-bit floating point
Single 32-bit floating point
String Unicode encoded string
Char Unicode encoded character, 16 Bit
Byte Unsigned character, 8bit
Decimal Decimal number, 128bit
Array Array
Xml Xml Object
Hashtable Hash table
Bool True and false

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

$strA = “Hello “ <enter>

$strB = “World!” <enter>

$strC = $strA + $strB <enter>

$strC <enter>

There are other methods for string operations, such:

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, it is very simple to operate a number without too much explanation. There are only a few examples:

5 + 100 <enter>

$x=200+1 <enter>

$x <enter>

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

$y <enter>

Sometimes, PowerShell cannot automatically specify the data type for us. Therefore, when writing a script, declare the data type for the variable as much as possible to prevent errors. The following operations are performed on mathematical operations:

+ Add
- Subtraction
* Multiplication
/ Division
% Remainder
= Assignment
++ Add 1 to the variable, equivalent to + 1
-- Subtract 1 from the variable, which is equivalent to-1.

Of course, parentheses are inevitable.

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.