How do I negate a condition in PowerShell?

后端 未结 4 1800
轻奢々
轻奢々 2020-12-23 08:37

How do I negate a conditional test in PowerShell?

For example, if I want to check for the directory C:\\Code, I can run:

if (Test-Path C:\\Code){
  w         


        
4条回答
  •  遥遥无期
    2020-12-23 09:16

    You almost had it with Not. It should be:

    if (-Not (Test-Path C:\Code)) {
        write "it doesn't exist!"
    } 
    

    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.

    if ((test-path C:\code) -bxor 1) {write "it doesn't exist!"}
    

提交回复
热议问题