I. Arithmetic Operators:
Operator |
DescriptionMing |
Example |
EndResult |
+ |
Add integers; Concatenates strings, arrays, and hash tables. A hash table cannot have the same key. |
1 + 1 "ABC" + "def" "ABC" + 123 "A", "B" + "C" "A", "B" + 1 "A", "B" + 1, 2 "A", "B" + "C", "D" @ {A = 1; B = 2} + @ {c = "cc"; D = "DD "} "A", "B", "C" + @ {A = "AA"; B = "BB "} |
2 Abcdef ABC123 A, B, c A, B, 1 A, B, 1, 2 A, B, c, d @ {A = 1; B = 2; C = "cc"; D = "DD "} A, B, C, @ {A = "AA"; B = "BB "} |
- |
Subtract one value from the other. Negative for numbers. |
1-1 -1-1 |
0 -2 |
* |
Multiply integers; Copies strings and arrays as specified times. |
2*3 "A" * 3 "A", "B" * 3 |
6 Aaa A B |
/ |
Divide two values. |
6/2 |
3 |
% |
Returns the remainder of the division operation. |
7% 2 |
1 |
Operator priority windows powershell processes arithmetic operators in the following order:
1. Parentheses ()
2.-(negative)
3. *,/, %
4. +,-(subtraction)
Ii. assignment operators:
Operator |
DescriptionMing |
Example |
= |
Set the variable value to the specified value. The variable name must start with $. The priority of the value assignment operator (=) is lower than that of the pipeline operator (|) You can convert kilobytes (Kb), megabytes (MB), and gigabytes (GB) to bytes. You can use a single command to assign values to multiple variables. By placing the type name in the variable name or square brackets before the first value, you can convert the new scalar variable to any. NET Framework type. You can also use the set-variable cmdlet to set variable values in addition to value assignment. |
$ Var = "test" $ Var = 1 $ Var = 1, 2, 3 $ Var = "A", "B", "C" $ Var =@{ A = 1; B = 2; C = "CCC "} $ Var = 1kb $ Var = 1 MB $ Var = 1 GB $ Var = 1 Tb $ A, $ B, $ c = 1, 2, 3 [Int] $ Var = 1 [String] $ Var = 1 [Int [] $ Var = 1, 2, 3 [System. datetime] $ Var = Get-Date Set-variable-Name a-value 1, 2, 3 |
+ = |
Adds a specified value to the value of a variable or adds the specified value to an existing value. |
Omitted |
-= |
Reduce the value of a variable by a specified value. |
Omitted |
* = |
Multiply the value of a variable with a specified value or append the specified value to an existing value. |
Omitted |
/= |
Divide the value of a variable by the specified value. |
Omitted |
% = |
Divide the value of a variable by the specified value, and assign the remainder (Modulo) to the variable. |
Omitted |
++ |
Increase the value of a variable, an attribute that can be assigned a value, or an array element by 1. |
Omitted |
-- |
Reduce the value of a variable, an attribute that can be assigned a value, or an array element by 1. |
Omitted |
Iii. Comparison Operators:
Operator |
DescriptionMing |
Example |
EndResult |
-EQ |
Equal. Contains the same value. |
1-EQ 1 "ABC"-EQ "ABC" 1, 2, 3-EQ 2 1, 2, 3, 1, 2, 2, 3-EQ 2 "ABC", "def"-EQ "ABC |
True True 2 2, 2 ABC |
-Ne |
Not equal. Contains a different value. |
1-ne 1 1, 2, 3-ne 1 "ABC", "def"-ne "ABC" |
False 2, 3 Def |
-GT |
(>) Greater |
1-GT 1 |
False |
-Ge |
(> =) Greater than or equal |
1-ge 1 |
True |
-Lt |
( |
1-LT 1 |
False |
-Le |
( |
1-LE 1 |
True |
-Like |
Use wildcard (*) to match. |
"ABC"-like "* B *" |
True |
-Notlike |
The wildcard (*) does not match. |
"ABC"-notlike "* B *" |
False |
-Match |
When the operator input (left-side parameter) is a Single Scalar object, the-match and-notmatch operators fill in $ matches automatic variables. When the input is a scalar, the-match and notmatch operators return a Boolean value and set the value of $ matches automatic variable to the matching item of the parameter. If the input is a set, the-match and-notmatch operators return matching members in the set, but the operator does not fill in the $ matches variable. |
123-match "^ [/d] + $" "ABC"-match "^ [/W] + $" "ABC", 123-match "^ [/d] +" $" |
True ($ Matches is 123) True ($ Matches is ABC) 123 ($ Matches is empty) |
-Notmatch |
Regular Expression comparison |
123-notmatch "^ [/d] + $" |
False |
-Contains |
Include operators. Contains the same complete value (not part of the value ). Returns a Boolean value. |
"ABC", "def"-contains "ABC" "ABC", "def"-contains "AB" "ABC", "def"-contains "AB *" |
True False False |
-Notcontains |
Include operators. Does not contain an identical value. Returns a Boolean value. |
"ABC", "def"-notcontains "ABC" |
False |
-Replace |
Replace with regular expressions |
"ABC"-Replace "B ","-" "A1b2c3"-Replace "/D ","-" |
A-C A-B-c- |
By default, all comparison operators are case-insensitive. To make a comparison operator case-sensitive, add the letter "C" before the Operator name ". For example, "-EQ" is case sensitive in the form of "-CEQ ". To explicitly indicate case insensitive, add the letter "I" before the operator ". For example, the explicit and case-insensitive form of "-EQ" is "-ieq ".
Input of all comparison operators except the include operator (-contains,-notcontains) and type operator (-is,-isnot) in the operator (value on the left of the operator) if it is a single value (scalar), a Boolean value is returned. When the input is a set of values, the include and type operators return any matching values. If there are no matching items in the Set, these operators do not return any value. Contains operators and type operators always return boolean values.
Iv. logical operators:
Operator |
DescriptionMing |
Example |
EndResult |
-And |
Logic and. True is true only when both statements are true. |
$ True-and $ false $ True-and $ true (1-EQ 1)-and (1-EQ 2) |
False True False |
-Or |
Logic or. True when either of the statements is true or both are true. |
$ True-or $ false (1-EQ 1)-Or (1-EQ 2) |
True True |
-XOR |
Logic exclusive or. It is true only when one statement is true and the other statement is false. |
$ True-XOR $ false $ True-XOR $ true |
True False |
-Not ! |
Non-logical. Returns the inverse of the following statement. |
-Not $ true ! $ True |
False False |
If the left operand of a statement containing the and operator is false, the right operand is not calculated. If the left operand of a statement containing the OR operator is true, the right operand is not calculated. Therefore, you can use these statements like if statements.
5. Redirection Operators:
Operator |
DescriptionMing |
Example |
> |
Sends the output to the specified file. |
Get-process> process.txt |
> |
Append the output content to the specified file. |
Dir *. PS1> scripts.txt |
2> |
Sends errors to the specified file. |
Get-process none 2> errors.txt |
2> |
Append the error to the content of the specified file. |
Get-process none 2> save-errors.txt |
2> & 1 |
Send the error to the successful output stream. |
Get-process none, powershell 2> & 1 |
If the specified file already exists, the redirection operators (> and 2>) that do not append data overwrite the current content of the file without warning. However, if the file is read-only, hidden, or system file, the redirection will fail. The append redirection operators (>>and 2>) do not write data to read-only files, but Append content to the system or hidden files.
To forcibly redirect content to read-only, hidden, or system files, use the out-file cmdlet with the force parameter. When writing a file, the redirection operator uses unicode encoding (output of less than five characters will be garbled ...). If the file uses different encodings, the output format may not be correctly set. To redirect content to a non-Unicode file, use the out-file cmdlet with the encoding parameter.
6. Split and join Operators:
Operator |
DescriptionMing |
Example |
EndResult |
-Split |
Split operator. The default delimiters are white spaces, including spaces and non-printable characters such as line breaks ('n') and tabs ('t. When splitting a string, the delimiters are omitted from all substrings. Specifies the maximum number of returned substrings. If the number of substrings is greater than the maximum number, the extra substrings are merged into the last substrings. The expression that specifies the delimiter to apply the rule. The calculation result of this expression must be $ true or $ false. The script block must be placed in braces. To use the unary join operator, enclose the string in parentheses. |
-Split "Red yellow blue green" "Lastname: firstname: Address"-split ":" "A1b2c"-split "/D" "Mercury, Venus, Earth"-split ",", 2 "Mercury, Venus, Earth"-split {$ _-EQ "E "} -Split ("1 2", "a B ") |
Red, yellow, blue, green Lastname, firstname, address A, B, c "Mercury", "Venus, Earth" M, "rcury, V", "NUS,", Arth 1, 2, a, B |
-Join |
Join operator. Concatenates a group of strings into a single string. |
-Join ("A", "B", "C ") "A", "B", "C"-join "-" |
ABC A-B-c |
In any split statement, you can replace-isplit or-csplit with-split. -The isplit and-split operators are case-insensitive. -The csplit operator is case sensitive, which means that the delimiter rules are applied in case sensitive.
VII. Type Operators:
Operator |
DescriptionMing |
Example |
EndResult |
-Is |
Returns true if the input is a specified. NET Framework instance. |
(Get-date)-Is [datetime] (Get-date)-is "datetime" 1, 2-is [array] |
True True True |
-Isnot |
Returns true if the input is not a specified. NET Framework instance. |
(Get-date)-isnot [datetime] |
False |
- |
Converts input to the specified. NET Framework type. |
"123"-as [int] |
123 |
8. bitwise operators:
Operator |
DescriptionMing |
Example |
EndResult |
-Band |
Bitwise AND |
10-band 3 |
2 |
-Bor |
Bit or |
10-bor 3 |
11 |
-Bxor |
Bitwise OR |
10-bxor 3 |
9 |
9. Special operators:
Operator |
DescriptionMing |
Example |
& |
Call operators. Run commands, scripts, or script blocks. Because the call operator is not analyzed, it cannot interpret the command parameters. This call operator is used to indicate that the value following it is a command. |
& "New cmdlet" $ C = "get-executionpolicy" & $ C |
. |
Attribute to cancel the reference operator. The attributes and methods of the access object. Use "." To obtain the source operator and run the script. Point (.) is also used as the parent directory symbol |
$ Mystring. Length $ Mystring. substring (1, 3) . C:/scripts. sample. PS1 ./Sample. PS1 |
:: |
Static member operator. Call the static property operators and methods of the. NET Framework class. To find the static attributes and methods of an object, use the static parameter of the get-member cmdlet. |
[Datetime]: Now |
.. |
Range operator. Indicates the sequence integer in the array of integers given the upper and lower limits. |
1. 10 10 .. 1 Foreach ($ A in 1 .. $ max) {write-host $} |
-F |
Format operator. Use the string object formatting method to format the string. Enter a format string on the left side of the operator and an object to be formatted on the right side of the operator. |
"{0} {1,-10} {2: n}"-F 1, "hello", [math]: pi |
$ () |
Subexpression operator. Returns the results of one or more statements. A scalar is returned for a single result. An array is returned for multiple results. |
$ ($ X * 23) $ (get-wmiobject win32_directory) |
@() |
Array subexpression operator. Returns the results of one or more statements through arrays. If there is only one entry, the array has only one member. |
@ (Get-wmiobject win32_logicaldisk) |
, |
When an operator is used as a binary operator, a comma is used to create an array. When used as the unary operator, comma is used to create an array containing only one member. |
$ Myarray = 1, 2, 3 $ Singlearray =, 1 |
There is nothing to say about this chapter. It should be remembered and will be used in the future.