如何否定PowerShell中的条件?
如何否定PowerShell中的条件testing?
例如,如果我想检查目录C:\ Code,我可以运行:
if (Test-Path C:\Code){ write "it exists!" } 有没有办法来否定这种情况,例如(非工作):
 if (Not (Test-Path C:\Code)){ write "it doesn't exist!" } 
解决办法 :
 if (Test-Path C:\Code){ } else { write "it doesn't exist" } 
这工作正常,但我更喜欢内联。
 你几乎和Not 。 它应该是: 
 if (-Not (Test-Path C:\Code)) { write "it doesn't exist!" } 
 你也可以用!  : if (!(Test-Path C:\Code)){} 
只是为了好玩,你也可以使用按位排他,虽然这不是最可读/可理解的方法。
 if ((test-path C:\code) -bxor 1) {write "it doesn't exist!"} 
如果你像我一样,不喜欢双括号,你可以使用一个函数
 function not ($cm, $pm) { if (& $cm $pm) {0} else {1} } if (not Test-Path C:\Code) {'it does not exist!'} 
例