如何在 PowerShell 中否定條件

  1. 使用 -not 運算符在 PowerShell 中否定一個條件
  2. 使用 ! 運算符在 PowerShell 中否定一個條件
  3. 使用 -notmatch 運算符在 PowerShell 中否定一個條件
  4. 使用 -ne 運算符在 PowerShell 中否定一個條件
  5. 使用 -notlike 運算符在 PowerShell 中否定一個條件
  6. 結論
如何在 PowerShell 中否定條件

PowerShell 有不同的決策語句來執行代碼,與其他程式語言類似。您可以在 PowerShell 腳本中使用條件進行決策。

這些腳本根據這些決策執行不同的動作。如果條件為 true,則會執行一個命令;如果條件為 false,則會執行另一個命令。

PowerShell 中最常用的語句之一是 If 語句。它有三種類型:if 語句、if-else 語句和嵌套的 if 語句。

PowerShell 還使用 switch 語句作為條件語句。

這裡是 if 語句的一個簡單示例。

if (5 -lt 7) {
    Write-Host "5 is less than 7"
}

如果 5 小於 7,則執行 Write-Host 命令。

輸出:

在 PowerShell 中否定一個條件 - 輸出 1

邏輯運算符連接 PowerShell 中的條件語句,這允許您測試多個條件。PowerShell 支持 -and-or-xor-not! 邏輯運算符。

本教程將教您如何在 PowerShell 中否定一個條件。

使用 -not 運算符在 PowerShell 中否定一個條件

在 PowerShell 中,-not 運算符是一個強大的工具,用於否定條件。它允許您輕鬆地反轉邏輯表達式的結果,使您的腳本更加動態和靈活。

$condition = $true
if (-not $condition) {
    Write-Host "Condition is false"
}

在這個示例中,我們將 $condition 初始化為 $true。當我們將 -not 運算符應用於 $condition 時,因為原始條件為真,所以它的值評估為 $false。因此,消息 "Condition is false" 不會被打印。

輸出:

在 PowerShell 中否定條件 - 輸出 2

現在,讓我們測試另一個條件。

$value = 5
if (-not ($value -eq 10)) {
    Write-Host "Value is not equal to 10"
}

在這個示例中,我們將 $value 設置為 5。我們使用 -eq 運算符來檢查 $value 是否等於 10,但使用 -not 運算符對該條件進行否定。因此,表達式 ($value -eq 10) 評估為 $false,而 -not 運算符將其轉換為 $true

結果,我們打印消息 "Value is not equal to 10"

輸出:

在 PowerShell 中否定條件 - 輸出 3

使用 ! 運算符在 PowerShell 中否定一個條件

! 運算符,也稱為 邏輯 NOT 運算符,用於否定一個條件。它基本上反轉布林表達式的真值。

當您需要檢查一個條件為假而不是為真時,這個運算符通常會被使用。

$condition = $true
if (!$condition) {
    Write-Host "Condition is false"
}

在這個示例中,我們將 $condition 初始化為 $true。當我們將 ! 運算符應用於 $condition 時,因為原始條件為真,所以它的值評估為 $false。因此,消息 "Condition is false" 不會被打印。

輸出:

在 PowerShell 中否定一個條件 - 輸出 4

使用 -notmatch 運算符在 PowerShell 中否定一個條件

在 PowerShell 中,-notmatch 運算符用於基於模式匹配否定一個條件。這個運算符檢查一個字符串是否不匹配指定的模式。

當您想要從選擇中排除某些字符串或模式時,它特別有用。

$string = "Hello, world!"
if ($string -notmatch "Goodbye") {
    Write-Host "String does not contain 'Goodbye'"
}

在這個示例中,我們有一個字符串 $string,其值為 "Hello, world!"。我們使用 -notmatch 運算符來檢查這個字符串是否不匹配模式 "Goodbye"

因為字符串 "Hello, world!" 不包含子字符串 "Goodbye",所以條件為真,消息 "String does not contain 'Goodbye'" 被打印。

輸出:

在 PowerShell 中否定一個條件 - 輸出 5

使用 -ne 運算符在 PowerShell 中否定一個條件

在 PowerShell 中,-ne 運算符用於基於不等式否定條件。它檢查兩個值是否不相等。

當您需要從比較中排除某些值時,這個運算符特別有用。

$value = 5
if ($value -ne 10) {
    Write-Host "Value is not equal to 10"
}

在這個示例中,我們將值 5 分配給變量 $value。然後使用 -ne 運算符檢查 $value 是否不等於 10

因為 $value 確實為 5,它不等於 10,條件評估為真,消息 "Value is not equal to 10" 被打印。

輸出:

在 PowerShell 中否定一個條件 - 輸出 6

使用 -notlike 運算符在 PowerShell 中否定一個條件

在 PowerShell 中,-notlike 運算符用於基於通配符模式匹配否定一個條件。它允許您檢查一個字符串是否不匹配指定的模式。

當您需要從比較中排除某些模式或子字符串時,這個運算符是有益的。

$string = "Hello, world!"
if ($string -notlike "*Goodbye*") {
    Write-Host "String does not contain 'Goodbye'"
}

在這個示例中,我們有一個字符串 $string,其值為 "Hello, world!"。我們使用 -notlike 運算符來檢查這個字符串是否不匹配模式 "*Goodbye*"

因為字符串 "Hello, world!" 不包含子字符串 "Goodbye",所以條件為真,消息 "String does not contain 'Goodbye'" 被打印。

輸出:

在 PowerShell 中否定一個條件 - 輸出 7

結論

在這篇文章中,我們探討了在 PowerShell 中否定條件的各種方法,提供了在腳本邏輯中靈活性和精確性。我們首先討論了 -not 運算符,它反轉邏輯表達式的結果,隨後是 ! 運算符,這是用於直接否定條件的 "邏輯 NOT" 運算符。

然後我們深入研究了 -notmatch 運算符,該運算符檢查字符串是否不匹配指定的模式,以及 -ne 運算符,該運算符評估兩個值的非等質性。最後,我們介紹了 -notlike 運算符,用於基於通配符模式匹配的否定條件。

通過理解和應用這些方法,PowerShell 使用者可以增強其腳本的健壯性和靈活性。

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
作者: Rohan Timalsina
Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website