Conditional operator _powershell for Windows PowerShell conditional expressions

Source: Internet
Author: User
Tags bitwise bitwise operators

Comparison operators in Powershell
-eq: Equal To
-ne: Not equal to
-GT: Greater Than
-ge: greater than or equal
-LT: Less than
-le: less than or equal
-contains: Contains
-notcontains: does not contain

For comparison

You can enter the comparison expression directly into the PowerShell console and then enter, automatically comparing and returning the comparison results.

Copy Code code as follows:

PS c:powershell> (3,4,5)-contains 2
False
PS c:powershell> (3,4,5)-contains 5
True
PS c:powershell> (3,4,5)-notcontains 6
True
PS c:powershell> 2-eq 10
False
PS c:powershell> "A"-eq "a"
True
PS c:powershell> "A"-ieq "a"
True
PS c:powershell> "A"-ceq "a"
False
PS c:powershell> 1gb-lt 1gb+1
True
PS c:powershell> 1gb-lt 1gb-1
False

Seek the opposite

The negation operator is-not but like a high-level language! "also supports negation.

Copy Code code as follows:

PS c:powershell> $a = 2-eq 3
PS c:powershell> $a
False
PS c:powershell>-not $a
True
PS c:powershell>! ($a)
True

Boolean operations

-and: And
-or: Or
-xor: Different or
-not: Inverse

Copy Code code as follows:

PS c:powershell> $true-and $true
True
PS c:powershell> $true-and $false
False
PS c:powershell> $true-or $true
True
PS c:powershell> $true-or $false
True
PS c:powershell> $true-xor $false
True
PS c:powershell> $true-xor $true
False
PS c:powershell>-not $true
False

Comparing arrays and collections

Filter the elements in an array

Copy Code code as follows:

PS c:powershell> 1,2,3,4,3,2,1-eq 3
3
3
PS c:powershell> 1,2,3,4,3,2,1-ne 3
1
2
4
2
1

Verify that an array has a specific element

Copy Code code as follows:

PS c:powershell> $help = (man ls)
PS c:powershell> 1,9,4,5-contains 9
True
PS c:powershell> 1,9,4,5-contains 10
False
PS c:powershell> 1,9,4,5-notcontains 10
True

A, value comparison

1)-eq: Equality (equal) operator
2) –ne: Inequality (not-equal) operator
3) –lt: Less Than (Less-than)
4) –GT: greater than (greater than)
5 Le: Less Than or equal (less-than-or-equal)
6 GE: Greater Than or equal to (greater-than-or-equal)
The value comparison operator can be used to compare two digits, or to compare two strings.
Note: String comparisons ignore case, and if you need a case sensitive comparison, you can use the operator-ceq,-CLT,-cle,-cge. When case sensitive comparisons, lowercase letters are less than uppercase letters.
The explicit case insensitive comparison operator in PowerShell is preceded by the prefix I, or-ieq,-ilt,-ile,-igt,-ige, of the default operator.

Two, implicit type conversion

The usual rule for automatic conversion in PowerShell is an expression consisting of two different types of variables, automatically converting the right-hand variable to the type of the left variable, and then evaluating the value of the expression.

Three, logical and bitwise operations

1 –and and operator, returns $true when the operands on both sides of the operator are $true.
2 –or or operator, returns $true when any operand is $true.
3 –xor or operator, if there is an operand is $true, then the expression returns $true, if two operands are $true, return $false.
4) –not or! Takes the inverse operator, which has only one operand, and acts to reverse it.
5 –band and-bor bitwise operations with (-band) and bitwise OR (-BOR) operators, only for integers.

Four, Boolean conversion

Multiple operators, including bitwise operators, return values of numeric types that can be automatically converted to Boolean-type values, and the conversion rule is that any Non-null value will be converted to $true PowerShell. The concept of Non-empty can be extended to a wider range, where the PowerShell will be implicitly converted to a Boolean value when needed. You can also explicitly perform a mandatory type conversion by adding [bool] to any value before the conversion rules are as follows:
1 Any non-0 value will be converted to $true
2) Non-0-length strings will be converted to $true
3 A collection of at least one item will return to $true
4 other objects will be converted to $true unless they are $null

The -like and-match string operators are true and can be used to detect whether a string consists of a particular pattern or whether it contains the required string form.

Vi. Sets and conditional expressions

PowerShell allows the collection to be used on the left side of the conditional expression. The shell interpretation engine will apply the conditional expression to members of the collection one at a a, and the result is a new collection of members that contain the returned truth.

Related Article

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.