How do I negate a conditional test in PowerShell?
For example, if I want to check for the directory C:Code, I can run:
Is there a way to negate that condition, e.g. (non-working):
This implicit casting happens all the time in PowerShell and (usually) helps make PowerShell behave the way you meant it to. But it can catch you off-guard if you’re not careful. PowerShell Portal; Wiki: Portal of TechNet Wiki Portals. You can also have Catch blocks that will only trap specific exceptions. The reason for doing this is so you can add different handlers for each possible failure condition that you may encounter. Some exceptions you may just want to log and exit, but others you may have a recovery action for.
Workaround:
This works fine, but I'd prefer something inline.
Peter Mortensen14.2k1919 gold badges8888 silver badges114114 bronze badges
Ben McCormackBen McCormack19.2k4444 gold badges123123 silver badges201201 bronze badges
2 Answers
You almost had it with
Not
. It should be:You can also use
!
: if (!(Test-Path C:Code)){}
Just for fun, you could also use bitwise exclusive or, though it's not the most readable/understandable method.
RynantRynant17.1k44 gold badges4545 silver badges6262 bronze badges
If you are like me and dislike the double parenthesis, you can use a function
Steven PennySteven Penny
Not the answer you're looking for? Browse other questions tagged windowspowershell or ask your own question.
-->SHORT DESCRIPTION
Describes the operators that connect statements in PowerShell.
LONG DESCRIPTION
The PowerShell logical operators connect expressions and statements, allowingyou to use a single expression to test for multiple conditions.
For example, the following statement uses the and operator and the or operatorto connect three conditional statements. The statement is true only when thevalue of $a is greater than the value of $b, and either $a or $b is less than20.
PowerShell supports the following logical operators.
Operator | Description | Example |
---|---|---|
-and | Logical AND. TRUE when both | (1 -eq 1) -and (1 -eq 2) |
statements are TRUE. | False | |
-or | Logical OR. TRUE when either | (1 -eq 1) -or (1 -eq 2) |
statement is TRUE. | True | |
-xor | Logical EXCLUSIVE OR. TRUE when | (1 -eq 1) -xor (2 -eq 2) |
only one statement is TRUE | False | |
-not | Logical not. Negates the statement | -not (1 -eq 1) |
that follows. | False | |
! | Same as -not | !(1 -eq 1) |
False |
Note:
The previous examples also use the equal to comparison operator
-eq
. Formore information, seeabout_Comparison_Operators. The examples alsouse the Boolean values of integers. The integer 0 has a value of FALSE. Allother integers have a value of TRUE.The syntax of the logical operators is as follows:
Statements that use the logical operators return Boolean (TRUE or FALSE)values.
The PowerShell logical operators evaluate only the statements required todetermine the truth value of the statement. If the left operand in a statementthat contains the and operator is FALSE, the right operand is not evaluated.If the left operand in a statement that contains the or statement is TRUE, theright operand is not evaluated. As a result, you can use these statements inthe same way that you would use the If statement.