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.