PowerShell If Statements

Leave a comment

PowerShell If statements need to be written in a particular syntax. Let’s look at some examples below.

Equals

In the example below, we test for equals. Note the notations -eq:

$a = 1
$b = 2
If ($a -eq $b)
{
    Write-Output "Equal"
}
Else
{
    Write-Output "Not Equal"
}

Greater Than

In the example below, we test for equals. Note the notations -gt:

$a = 1
$b = 2
If ($a -gt $b)
{
    Write-Output "Greater than"
}
Else
{
    Write-Output "Not greater than"
}

You can also use ge (greater than or equal to), lt, le etc.

Note the order can be switched.

Boolean

Let’s look at a true/false example below, which produces false:

$a = $false
If ($true -eq $a)
{
    Write-Output "true"
}
Else
{
    Write-Output "false"
}

 

Carl de Souza
Keep learning. Keep building.

Explore AI, agents & Microsoft technology.

I share practical ideas, tutorials, and videos about AI, AI agents, Microsoft technologies, and the Power Platform.

Subscribe on YouTube →
Carl de Souza Enterprise Architect at Microsoft · AI Technology Expert

Leave a Reply

Your email address will not be published. Required fields are marked *