PowerShell で条件を否定するにはどうすればいいですか? 質問する

PowerShell で条件を否定するにはどうすればいいですか? 質問する

PowerShell で条件テストを否定するにはどうすればよいですか?

たとえば、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"
}

これは問題なく動作しますが、インラインのものの方が好みです。

ベストアンサー1

ほぼ で終わりました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!"}

おすすめ記事