How to Use Exclamation Mark in PowerShell If Statements?

In one of the previous tutorials, I explained how to use the if-else statement in PowerShell. Now, I will show you how to use exclamation mark in PowerShell If Statements. An exclamation mark (!) serve as a logical negation operator; we can use it in PowerShell if statements.

Exclamation Mark in PowerShell If Statements

If you are new to an if statement, then here is the syntax of a PowerShell if statement.

if (condition) {
    # Code to execute if the condition is true
}

For example, consider a scenario where you want to check if a variable $city contains the value “New York”:

$city = "New York"

if ($city -eq "New York") {
    Write-Output "The city is New York."
}

In this example, the condition $city -eq "New York" evaluates to true, so the message “The city is New York.” is printed to the console.

Now, let me show you how to use the exclamation mark in PowerShell.

The exclamation mark (!) in PowerShell is used as a logical negation operator. It negates the value of a boolean expression. For instance, if a condition is true, applying ! will make it false, and vice versa.

Now, let us see some examples.

Example 1: Check if Not Exist

Here is an example to check if the city is not New York:

$city = "Los Angeles"

if ($city -ne "New York") {
    Write-Output "The city is not New York."
}

In this case, $city -ne "New York" evaluates to true because the value of $city is “Los Angeles,” which is not equal to “New York.” Therefore, the message “The city is not New York.” is printed.

Now, let us see how to achieve the same result using the exclamation mark in the if statement in PowerShell:

$city = "Los Angeles"

if (!($city -eq "New York")) {
    Write-Output "The city is not New York."
}

Here, !($city -eq "New York") negates the condition $city -eq "New York". Since $city is “Los Angeles,” the condition $city -eq "New York" evaluates to false, and applying ! makes it true. Thus, the message “The city is not New York.” is printed.

You can see the output in the screenshot below:

Exclamation Mark in PowerShell If Statements

Read PowerShell If Else Statement to Check if a Number is Between Two Values

Example 2: Check if a File Exists in an If Statement with an Exclamation Mark

Here is another example of checking whether a file exists on your system and performing an action if it does not. In PowerShell, you can use the exclamation mark in the if-else statement.

$filePath = "C:\MyFolder\report.pdf"

if (!(Test-Path $filePath)) {
    Write-Output "The file does not exist."
    # Code to create the file or handle the absence
}

In this example, Test-Path $filePath checks if the file exists. If the file does not exist, Test-Path returns false, and !(Test-Path $filePath) evaluates to true, triggering the message “The file does not exist.”

After executing it using VS code, here is the output of the above PowerShell script in the screenshot below.

Use Exclamation Mark in PowerShell If Statements

Example 3: Validate User Input using an Exclamation Mark

Suppose you are writing a script that prompts the user to enter their state of residence, and you need to ensure that they do not leave the input blank.

So, here, how can you use the exclamation mark in the PowerShell if statements.

$state = Read-Host "Enter your state of residence"

if (!($state)) {
    Write-Output "You must enter a state."
    # Code to prompt the user again or handle the empty input
}

In this case, !($state) checks if the $state variable is empty or null. If the user does not enter any value, $state will be an empty string, and !($state) will evaluate to true, prompting the message “You must enter a state.”

Read How to Use Multiple Conditions in PowerShell If Else Statement?

Example 4: Check Multiple Conditions using the Exclamation Mark

PowerShell also allows you to combine multiple conditions in an if statement using logical operators such as -and-or, and -not. The exclamation mark can be used with these operators to create complex conditions.

Here is an example to check if a user is not an administrator and their account is not locked using an exclamation mark in an if statement.

$isAdmin = $false
$isLocked = $false

if (!($isAdmin) -and !($isLocked)) {
    Write-Output "The user is not an administrator and their account is not locked."
    # Code to grant access or perform other actions
}

In this example, !($isAdmin) checks if the user is not an administrator, and !($isLocked) checks if the account is not locked. Both conditions must be true for the message “The user is not an administrator, and their account is not locked.” to be printed.

Here is the output in the screenshot below:

Check Multiple Conditions using Exclamation Mark in if statement powershell

Example 5: Negate a Complex Condition

Here is an example of how to use the $() syntax to evaluate a complex expression and return its result. And most importantly, we will see how to use this with the exclamation mark in if statements.

Suppose you need to check if a user is not in a specific group and the user’s account is not expired:

$userGroups = @("Users", "PowerUsers")
$isExpired = $false

if (!($userGroups -contains "Administrators" -and !$isExpired)) {
    Write-Output "The user is either not in the Administrators group or their account is expired."
}

In this example, !($userGroups -contains "Administrators" -and !$isExpired) negates the entire condition. Suppose the user is not in the “Administrators” group or their account is expired. In that case, the message “The user is either not in the Administrators group or their account is expired.” is printed.

Read PowerShell If-Else String Comparison

Example 6: Check User Permissions using the Exclamation Mark in the If Statement

Suppose you need to verify if a user does not have specific permissions before allowing them to perform an action. Then, you can write the PowerShell script below.

$userPermissions = @("Read", "Write")

if (!($userPermissions -contains "Admin")) {
    Write-Output "The user does not have Admin permissions."
    # Code to restrict access or notify the user
}

In this example, !($userPermissions -contains "Admin") checks if the “Admin” permission is not present in the user’s permissions array.

Example 6: Check if a Service is Not Running

Here is another example: you want to ensure that a specific service is not running before performing maintenance tasks. Below is the PowerShell script. I used the exclamation mark in the If Statement.

$serviceName = "Spooler"
$serviceStatus = (Get-Service -Name $serviceName).Status

if (!($serviceStatus -eq "Running")) {
    Write-Output "The service is not running. Safe to proceed with maintenance."
    # Code to perform maintenance tasks
}

Here, !($serviceStatus -eq "Running") checks if the service status is not “Running,”.

Conclusion

Now, I hope you learn how to use the exclamation mark (!) in a PowerShell if-else statement. We saw six different examples of how to effectively use the exclamation mark with if statements in PowerShell.

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.