This series of blogs from this section is PowerShell's grammar knowledge, before beginning to learn the grammar, I hope you have a basic understanding of PowerShell, such as the use of development tools, object-oriented features, detailed content of the hard Poke here (1)-(6) of the content.
This section mainly describes the arithmetic operators in PowerShell.
Operator
|
Usage
|
Example |
| Precautions
+ |
add integers; concatenate strings, arrays, and hash tables . |
2+5, "str1" + "str2" |
Whether PowerShell is a concatenated arithmetic addition or a string, whichever is the type of the first operand |
- |
Subtract two values |
2-1; (get-date). Day-1
|
|
-
|
Negative logarithm of a word |
-2+1 |
|
*
|
Multiply an integer by a specified number of times to copy strings and arrays |
2*3; "Str" |
Returns multiple copies of an input string |
/ |
Divide two values into one |
4/2 |
|
% |
Rest (returns the remainder of the division operation) |
|
|
The following are the precedence of these arithmetic operators
Parentheses () > Negative (-) > *,/,% > Add and Subtract
Note: If you remember that these priorities are too challenging, just know that the parentheses have the highest precedence. do not write code (scripts or commands) that are ambiguous in the sequence of operations. The sign of clarity is that you should be able to accurately express the order of arithmetic operations at any time.
The following is an example of using the arithmetic operator in PowerShell
ps c:\documents and settings\administrator> 1+23ps c:\documents and settings\administrator> 2-11ps c:\documents and settings\administrator> -7+ 4-3ps c:\documents and settings\administrator> 3*721ps c:\documents and Settings\administrator> 7/32.33333333333333ps c:\documents and settings\administrator > 7%31PS C:\Documents and Settings\Administrator> "STR" *2strstrps c:\ documents and settings\administrator> "Windows" + " " + "Powershell" windows powershellps c:\documents and settings\administrator> 2* "str" "*" operator failed: unable to value "str" Convert to Type "System.Int32". Error: "The input string is not properly formatted. ”。 Location Line:1 character: 3+ 2* <<<< "str" + categoryinfo : invalidoperation: (:) [],&nbSp Runtimeexception + fullyqualifiederrorid : operatorfailed
PowerShell addition and multiplication are not strictly exchangeable
As we mentioned above, for the operator "+", PowerShell is the addition or the string connection is based on the type of the first operand. As an example,
PS C:\Documents and settings\administrator> "File1" +1file11ps C:\Documents and settings\administrator> 1+ "File1" The value "File1" cannot be converted to type ' System.Int32 '. Error: "The input string is not properly formatted. "Location line: 1 characters: 1+ <<<<" file1 "+ categoryinfo:notspecified: (:) [], RuntimeException + fullyqualifiederrorid:runtimeexception
For the multiplication operator "*", the same as the type of the first operand
PS C:\Documents and settings\administrator> "str" *2strstrps C:\Documents and settings\administrator> "str" "*" Operator failed: Could not convert the value "str" to Type "System.Int32". Error: "The input string is not properly formatted. ”。 Location Line: 1 characters: <<<< "str" + categoryinfo:invalidoperation: (:) [], RuntimeException + fullyqualifiederrorid:operatorfailed
From the results shown above, in PowerShell, addition and multiplication are not strictly exchangeable, in other words, PowerShell(A + B) is not always equal to (b + a), and (A * b) is not always equal to (b * a).
In addition, the addition "+" array and hash table in PowerShell can also be manipulated and discussed in detail in the array section.
This article from "Flower Blossom Fall" blog, declined reprint!
(7) PowerShell arithmetic operators