PowerShell If Variable Equals [With Examples]

In PowerShell, if statements are used to control the flow of execution based on the evaluation of conditional expressions. In this tutorial, I will explain how to use if statements to check if a variable equals a specific value with examples.

In PowerShell, you can check if a variable equals a specific value using the -eq operator. For example, to verify if a variable $status equals “Active,” you would write:

$status = "Active"

if ($status -eq "Active") {
    Write-Output "The status is Active."
} else {
    Write-Output "The status is not Active."
}

This if statement evaluates whether $status is “Active” and executes the corresponding block of code based on the result.

PowerShell If Variable Equals

The “if variable equals” statement in PowerShell is used to execute a block of code only if a specified condition is true. This condition often involves comparing a variable to a specific value using the -eq operator, which checks for equality.

Here’s the basic syntax for the “if variable equals” statement in PowerShell:

if ($variable -eq $value) {
    # Code to execute if $variable equals $value
}

In PowerShell, the -eq operator is used to perform an equality check between two values. If the values are equal, the condition evaluates to $true, and the code block inside the if statement is executed. If the values are not equal, the condition evaluates to $false, and the code block is skipped.

Now, let me show you some examples.

Example 1: Check User Role

In PowerShell, you can check if a variable equals a specific value using the -eq operator. This operator is case-insensitive by default. Here’s a simple example:

Suppose you manage a system where you must perform different actions based on the user role. Here’s how you can use the “if variable equals” statement to check if a user is an administrator:

$userRole = "Admin"

if ($userRole -eq "Admin") {
    Write-Output "Welcome, Administrator!"
} else {
    Write-Output "Access Denied. You do not have administrative privileges."
}

In this example, if the $userRole variable equals “Admin”, a welcome message is displayed. Otherwise, an access denied message is shown.

Here is the output in the screenshot below:

PowerShell If Variable Equals

Here is another example of User Role Validation:

Suppose you are managing a system where users have different roles, and you need to perform specific actions based on the user’s role. Here’s how you can use if statements to check the user role:

$userRole = "Admin"

if ($userRole -eq "Admin") {
    Write-Output "Granting access to admin features."
} elseif ($userRole -eq "Editor") {
    Write-Output "Granting access to editor features."
} elseif ($userRole -eq "Viewer") {
    Write-Output "Granting access to viewer features."
} else {
    Write-Output "Role not recognized. Access denied."
}

Check out Check if a Variable is Empty in PowerShell

Example 2: Multiple Conditions with elseif

You can use elseif to handle multiple conditions. For example, checking the status of a service and its load:

$serviceStatus = "Running"
$serviceLoad = 75

if ($serviceStatus -eq "Stopped") {
    Write-Output "The service is stopped."
} elseif ($serviceStatus -eq "Running" -and $serviceLoad -gt 80) {
    Write-Output "The service is running but under high load."
} else {
    Write-Output "The service is running normally."
}

In this example, the script checks if the service is stopped, running under high load, or running normally.

Here is the output in the screenshot below:

PowerShell If Variable Equals Example

Check out Check if a Variable Exists in PowerShell

Example 3: Using -ne for Inequality

The -ne operator checks for inequality in PowerShell if variables. Here’s an example where you need to ensure a variable does not equal a specific value:

$userName = "JohnDoe"

if ($userName -ne "Admin") {
    Write-Output "Hello, $userName. You are not an administrator."
} else {
    Write-Output "Welcome, Administrator!"
}

This script checks if the $userName variable does not equal “Admin” and displays a corresponding message.

Check out Pass Variables to a PowerShell Script

Example 4: Compare Multiple Values

Sometimes, you may need to check if a variable equals one of several values. You can achieve this using multiple -or conditions:

$variable = "PowerShell"

if ($variable -eq "PowerShell" -or $variable -eq "Bash" -or $variable -eq "Python") {
    Write-Output "The variable is a recognized scripting language."
} else {
    Write-Output "The variable is not a recognized scripting language."
}

Alternatively, you can use an array and the -contains operator for a cleaner approach:

$variable = "PowerShell"
$languages = @("PowerShell", "Bash", "Python")

if ($languages -contains $variable) {
    Write-Output "The variable is a recognized scripting language."
} else {
    Write-Output "The variable is not a recognized scripting language."
}

Conclusion

In this tutorial, I have explained everything about PowerShell If Variable Equals with examples.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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