Introduction to Conditions

Overview

We previously mentioned that you can perform various types of operations on values and variables. Sometimes, before performing an operation, you want to examine one or more conditions. The reason is that you want to find out whether a certain condition is true or false and then proceed based on the result of that condition.

When you perform a comparison, such as between two numbers, you probably want to find whether those numbers are the same, whether they are different, or one is greater than the other. To perform such a comparison, you use two operands and an operator as in the following formula:

operand1 operator operand2

This is the basic formula of a Boolean expression.

Introduction to Boolean Values

A value is said to be Boolean if it is true or it is false. In fact, the two available Boolean values are $true and $false.

A Boolean Variable

To declare a Boolean variable, start with $ followed by a letter or a combination of characters that start with a letter. To initialize a Boolean variable, assign $true or $false to it. Here is an example:

$drinkingUnderAge = $true

Presenting a Boolean Value

To display the value of a Boolean variable, you can type its name at the PowerShell prompt or at the terminal of Visual Studio Code. Here are examples:

Assigning a Logical Expression to a Boolean Variable

We saw that, to initialize or specify the value of a Boolean variable, you can assign a $true or $false value to it. A Boolean variable can also be initialized with a Boolean expression. This would be done as follows:

Boolean-variable = operand1 Boolean-operator operand2

To make your code easy to read, you should include the logical expression in parentheses. This can be done as follows:

Boolean-variable = (operand1 Boolean-operator operand2);

If a Condition Applies

Introduction

The primary formula to perform a comparison is with an operator named if. The primary formula to use it is:

if(condition) { statement }

The condition can have the following formula:

operand1 Boolean-operator operand2

Any of the operands can be a constant or the name of a variable. The operator is a logical one. The whole expression is the condition. If the expression produces a true result, then the statement would execute.

The Body of a Conditional Statement

The statement is called the body of the conditional statement. It must start with an opening curly bracket "{" and end with a closing curly bracket "}". This would be done as follows:

if( operand1 operator operand2)
{
    . . .
    . . .
    . . .
}

In this case, the section from { (included) to } (included) is referred to as the body of the conditional statement.

Fundamentals of Logical Operators

Introduction

As introduced in previous sections, a logical comparison is used to establish the logical relationship between two values, a variable and a value, or two variables. There are various operators available to perform such comparisons. Consider the following code from a file named Exercise.ps1:

[string]$firstName   = 'Michael'
[string]$lastName    = 'Carlock'
[float]$hSalary     = 24.77
[float]$timeWorked  = 38.50
[float]$netPay      = $hSalary * $timeWorked
Write-Host '==============================='
Write-Host 'Payroll Evaluation'
Write-Host '==============================='
Write-Host 'Employee Information'
Write-Host '-------------------------------'
Write-Host "Full Name:     $firstName $lastName"
Write-Host "Hourly Salary: $hSalary"
Write-Host '==============================='
Write-Host "Time Worked:   $timeWorked"
Write-Host "Net Pay:       $netPay"
Write-Host '==============================='

This would produce:

PS C:\Windows\System32> C:\Exercises\Conditions.ps1
===============================
Payroll Evaluation
===============================
Employee Information
-------------------------------
Full Name:     Michael Carlock
Hourly Salary: 24.77
===============================
Time Worked:   38.5
Net Pay:       953.645
===============================

A Comparison for Equality

To compare two values or two variables for equality, you can use an operator named -eq. The formula to use it is:

value1 -eq value2

The equality operation is used to find out whether two variables (or one variable and a constant) hold the same value. Here is an example:

[ushort]$startingSalary   = 34000
[string]$employmentStatus = "Full-Time"

if($employmentStatus -eq "Full-Time") {
    $startingSalary = 40000
}

Write-Host 'Employee Record'
Write-Host '----------------------------------'
Write-Host 'Employment Status:     ' $employmentStatus
Write-Host 'Starting Yearly Salary:' $startingSalary
Write-Host '=================================='

This would produce:

PS C:\Windows\System32> C:\Exercises\Conditions.ps1
Employee Record
----------------------------------
Employment Status:      Full-Time
Starting Yearly Salary: 40000
==================================

A Difference in Values

To find out whether two values are different, use an operator named -ne. Here is an example:

$certification    = "y"
$employmentStatus = "Hired"

if($certification -ne "y") {
    $employmentStatus = "This employment requires a security certification. We will get back to you."
}

Write-Host '=================================================='
Write-Host 'Employment Verification'
Write-Host '--------------------------------------------------'
Write-Host "Candition holds certification:" $certification
Write-Host "Decision Status:              " $employmentStatus
Write-Host '=================================================='

This would produce:

PS C:\Windows\System32> C:\Exercises\Conditions.ps1
==================================================
Employment Verification
--------------------------------------------------
Candition holds certification: y
Decision Status:               Hired
==================================================

A Value Less Than Another

To find out whether one value is lower than another, you use an operator named -lt. Here are examples:

[ubyte]$discountRate = 75
[string]$itemName        = 'Pleated Red Dress'
[float]$originalPrice   = 224.95
[ubyte]$daysInStore     = 45

if($daysInStore -lt 60) {
    $discountRate = 50
}

if($daysInStore -lt 45) {
    $discountRate = 35
}

if($daysInStore -lt 35) {
    $discountRate = 15
}

if($daysInStore -lt 15) {
    $discountRate = 0
}

$discountAmount  = $originalPrice * $discountRate / 100
$discountedPrice = $originalPrice - $discountAmount

Write-Host '======================================================='
Write-Host 'Store Inventory'
Write-Host '-------------------------------------------------------'
Write-Host "Item Name:       " $itemName
Write-Host "Original Price:  " $originalPrice
Write-Host "Days in Store:   " $daysInStore
Write-Host "Discount Rate:   " $discountRate%
Write-Host "Discount Amount: " $discountAmount
Write-Host "Discounted Price:" $discountedPrice
Write-Host '======================================================='

This would produce:

PS C:\Windows\System32> C:\Exercises\Conditions.ps1
=========================
Store Inventory
-------------------------------------
Item Name:        Pleated Red Dress
Original Price:   224.95
Days in Store:    45
Discount Rate:    50%
Discount Amount:  112.475
Discounted Price: 112.475
=========================

A Value Less Than Or Equal To Another

To find out whether a certain value is less than or equal to another, you use an operator named -le. Here is an example:

$firstName    = "Catherine"
$lastName     = "Busbey"
$hSalary      = 24.37

#Time Worked"
$monday       = 9.5
$tuesday      = 8
$wednesday    = 10.5
$thursday     = 9.5
$friday       = 10

$timeWorked = $monday + $tuesday + $wednesday + $thursday + $friday

$regTime      = 40.00
$regPay       = $hSalary * 40.00
$overtime     = $timeWorked - 40.00
$overPay      = $hSalary * 1.50 * $overtime
        
if($timeWorked -le 40.00) {
    $regTime  = $timeWorked
    $regPay   = $hSalary * $timeWorked
    $overtime = 0.00
    $overPay  = 0.00
}

$netPay = $regPay + $overPay

Write-Host '========================================='
Write-Host 'Payroll Evaluation'
Write-Host '========================================='
Write-Host 'Employee Information'
Write-Host '-----------------------------------------'
Write-Host "Full Name:     $firstName $lastName}"
Write-Host "Hourly Salary: $hSalary"
Write-Host '========================================='
Write-Host 'Time Worked Summary'
Write-Host '-----------------------------------------'
Write-Host "Monday:        $monday"
Write-Host "Tuesday:       $tuesday"
Write-Host "Wednesday:     $wednesday"
Write-Host "Thursday:      $thursday"
Write-Host "Friday:        $friday"
Write-Host '========================================='
Write-Host '    Pay Summary'
Write-Host '-----------------------------------------'
Write-Host '                     Time   Pay'
Write-Host '-----------------------------------------'
Write-Host "    Regular:         $regTime   $regPay"
Write-Host '-----------------------------------------'
Write-Host "    Overtime:        $overtime   $overPay"
Write-Host '========================================='
Write-Host "    Net Pay:         $netPay"
Write-Host '========================================='

This would produce:

PS C:\Windows\System32> C:\Exercises\Conditions.ps1
=========================================
Payroll Evaluation
=========================================
Employee Information
-----------------------------------------
Full Name:     Catherine Busbey}
Hourly Salary: 24.37
=========================================
Time Worked Summary
-----------------------------------------
Monday:        9.5
Tuesday:       8
Wednesday:     10.5
Thursday:      9.5
Friday:        10
=========================================
    Pay Summary
-----------------------------------------
                     Time   Pay
-----------------------------------------
    Regular:         40   974.8
-----------------------------------------
    Overtime:        7.5   274.1625
=========================================
    Net Pay:         1248.9625
=========================================

A Value Greater Than Another: >

To find out if one value is greater than the other, you use an operator named -gt. The formula to use it is:

value1 -gt value2

Both operands, in this case value1 and value2, can be variables or the left operand can be a variable while the right operand is a constant. If the value on the left of the -gt operator is greater than the value on the right side or a constant, the comparison produces a True result. Otherwise, the comparison renders False. Here is an example:

$firstName    = "Catherine"
$lastName     = "Busbey"
$hSalary      = 24.37

#Time Worked"
$monday       = 7
$tuesday      = 8
$wednesday    = 6.5
$thursday     = 8.5
$friday       = 6.5

$timeWorked = $monday + $tuesday + $wednesday + $thursday + $friday

$regTime  = $timeWorked
$overtime = 0.00
$overPay  = 0.00
$regPay   = $hSalary * $timeWorked

if($timeWorked -gt 40.00) {
    $regTime  = 40.00
    $regPay   = $hSalary * 40.00
    $overtime = $timeWorked - 40.00
    $overPay  = $hSalary * 1.50 * $overtime
}

$netPay = $regPay + $overPay

Write-Host '========================================='
Write-Host 'Payroll Evaluation'
Write-Host '========================================='
Write-Host 'Employee Information'
Write-Host '-----------------------------------------'
Write-Host "Full Name:     $firstName $lastName"
Write-Host "Hourly Salary: $hSalary"
Write-Host '========================================='
Write-Host 'Time Worked Summary'
Write-Host '-----------------------------------------'
Write-Output "Monday:        $monday"
Write-Output "Tuesday:       $tuesday"
Write-Output "Wednesday:     $wednesday"
Write-Output "Thursday:      $thursday"
Write-Output "Friday:        $friday"
Write-Host '========================================='
Write-Host '    Pay Summary'
Write-Host '-----------------------------------------'
Write-Host '                     Time   Pay'
Write-Host '-----------------------------------------'
Write-Output "    Regular:         $regTime   $regPay"
Write-Output '-----------------------------------------'
Write-Output "    Overtime:        $overtime   $overPay"
Write-Output '========================================='
Write-Output "    Net Pay:         $netPay"
Write-Host '========================================='

This would produce:

PS C:\Windows\System32> C:\Exercises\Conditions.ps1
=========================================
Payroll Evaluation
=========================================
Employee Information
-----------------------------------------
Full Name:     Catherine Busbey
Hourly Salary: 24.37
=========================================
Time Worked Summary
-----------------------------------------
Monday:        7
Tuesday:       8
Wednesday:     6.5
Thursday:      8.5
Friday:        6.5
=========================================
    Pay Summary
-----------------------------------------
                     Time   Pay
-----------------------------------------
    Regular:         36.5   889.505
-----------------------------------------
    Overtime:        0   0
=========================================
    Net Pay:         889.505
=========================================

Here is another version of the code with different values:

$firstName    = "Catherine"
$lastName     = "Busbey"
$hSalary      = 24.37

#Time Worked"
$monday       = 9.5
$tuesday      = 8
$wednesday    = 10.5
$thursday     = 9
$friday       = 10.5

$timeWorked = $monday + $tuesday + $wednesday + $thursday + $friday

$regTime  = $timeWorked
$overtime = 0.00
$overPay  = 0.00
$regPay   = $hSalary * $timeWorked

if($timeWorked -gt 40.00) {
    $regTime  = 40.00
    $regPay   = $hSalary * 40.00
    $overtime = $timeWorked - 40.00
    $overPay  = $hSalary * 1.50 * $overtime
}

$netPay = $regPay + $overPay

Write-Host '========================================='
Write-Host 'Payroll Evaluation'
Write-Host '========================================='
Write-Host 'Employee Information'
Write-Host '-----------------------------------------'
Write-Output "Full Name:     $firstName $lastName"
Write-Output "Hourly Salary: $hSalary"
Write-Host '========================================='
Write-Host 'Time Worked Summary'
Write-Host '-----------------------------------------'
Write-Host "Monday:        $monday"
Write-Host "Tuesday:       $tuesday"
Write-Host "Wednesday:     $wednesday"
Write-Host "Thursday:      $thursday"
Write-Host "Friday:        $friday"
Write-Host '========================================='
Write-Host '    Pay Summary'
Write-Output '-----------------------------------------'
Write-Output '                     Time   Pay'
Write-Output '-----------------------------------------'
Write-Output "    Regular:         $regTime   $regPay"
Write-Output '-----------------------------------------'
Write-Output "    Overtime:        $overtime   $overPay"
Write-Output '========================================='
Write-Output "    Net Pay:         $netPay"
Write-Output '========================================='

This would produce:

PS C:\Windows\System32> C:\Exercises\Conditions.ps1
=========================================
Payroll Evaluation
=========================================
Employee Information
-----------------------------------------
Full Name:     Catherine Busbey
Hourly Salary: 24.37
=========================================
Time Worked Summary
-----------------------------------------
Monday:        9.5
Tuesday:       8
Wednesday:     10.5
Thursday:      9
Friday:        10.5
=========================================
    Pay Summary
-----------------------------------------
                     Time   Pay
-----------------------------------------
    Regular:         40   974.8
-----------------------------------------
    Overtime:        7.5   274.1625
=========================================
    Net Pay:         1248.9625
=========================================

A Value Greater Than or Equal to Another

To find out whether one value is greater than or equal to another, you can use an operator named -ge, read the "greater than or equal to" operator. The formula it follows is:

value1 -ge value2

The comparison is performed on both operands: value1 and value2:

Options on Conditional Statements

if a Condition is True

One way to formulate a conditional statement is to find out whether a situation is true or false. In this case, one of the operands must be a Boolean value as $true or $false. The other operand can be a Boolean variable. One of the operands can also be an expression that holds a Boolean value.

Imagine that you want to evaluate the condition as being true. Here is an example:

$firstName    = "Catherine"
$lastName     = "Busbey"
$hSalary      = 24.37
$employeeIsFullTime = $true

#Time Worked"
$monday       = 9
$tuesday      = 8
$wednesday    = 10.5
$thursday     = 9.5
$friday       = 8.5

$timeWorked = $monday + $tuesday + $wednesday + $thursday + $friday

$regTime      = $timeWorked
$regPay       = $hSalary * $timeWorked
$overtime     = 0.00
$overPay      = 0.00

if($employeeIsFullTime -eq $true) {
    $regTime  = 40.00
    $regPay   = $hSalary * 40.00
    $overtime = $timeWorked - 40.00
    $overPay  = $hSalary * 1.50 * $overtime
}

$netPay = $regPay + $overPay

Write-Output '========================================='
Write-Host 'Payroll Evaluation'
Write-Output '========================================='
Write-Host 'Employee Information'
Write-Output '-----------------------------------------'
Write-Host "Full Name:     $firstName $lastName"
Write-Output "Hourly Salary: $hSalary"
Write-Host '========================================='
Write-Output 'Time Worked Summary'
Write-Host '-----------------------------------------'
Write-Output "Monday:        $monday"
Write-Host "Tuesday:       $tuesday"
Write-Output "Wednesday:     $wednesday"
Write-Host "Thursday:      $thursday"
Write-Output "Friday:        $friday"
Write-Host '========================================='
Write-Output '    Pay Summary'
Write-Host '-----------------------------------------'
Write-Output '                     Time   Pay'
Write-Host '-----------------------------------------'
Write-Output "    Regular:         $regTime   $regPay"
Write-Host '-----------------------------------------'
Write-Output "    Overtime:        $overtime   $overPay"
Write-Host '========================================='
Write-Output "    Net Pay:         $netPay"
Write-Host '========================================='

This would produce:

PS C:\Windows\System32> C:\Exercises\Conditions.ps1
=========================================
Payroll Evaluation
=========================================
Employee Information
-----------------------------------------
Full Name:     Catherine Busbey
Hourly Salary: 24.37
=========================================
Time Worked Summary
-----------------------------------------
Monday:        9
Tuesday:       8
Wednesday:     10.5
Thursday:      9.5
Friday:        8.5
=========================================
    Pay Summary
-----------------------------------------
                     Time   Pay
-----------------------------------------
    Regular:         40   974.8
-----------------------------------------
    Overtime:        5.5   201.0525
=========================================
    Net Pay:         1175.8525
=========================================

If a Boolean variable is (currently) holding a true value (at the time you are trying to access it), when you are evaluating the expression as being $true, you can omit the -eq $true or the $true -eq expression in your statement. Therefore, the above expression can be written as follows:

. . .

$timeWorked = $monday + $tuesday + $wednesday + $thursday + $friday

$regTime      = $timeWorked
$regPay       = $hSalary * $timeWorked
$overtime     = 0.00
$overPay      = 0.00

if($employeeIsFullTime) {
    $regTime  = 40.00
    $regPay   = $hSalary * 40.00
    $overtime = $timeWorked - 40.00
    $overPay  = $hSalary * 1.50 * $overtime
}

$netPay = $regPay + $overPay

. . .

This would produce the same result as previously.

Negating a Condition

Consider the following code:

$firstName    = "Catherine"
$lastName     = "Busbey"
$hSalary      = 24.37
$employeeIsFullTime = $false

#Time Worked"
$monday       = 10
$tuesday      = 9
$wednesday    = 8.5
$thursday     = 10.5
$friday       = 8

$timeWorked = $monday + $tuesday + $wednesday + $thursday + $friday

$regTime      = $timeWorked
$regPay       = $hSalary * $timeWorked
$overtime     = 0.00
$overPay      = 0.00

if($employeeIsFullTime) {
    $regTime  = 40.00
    $regPay   = $hSalary * 40.00
    $overtime = $timeWorked - 40.00
    $overPay  = $hSalary * 1.50 * $overtime
}

$netPay = $regPay + $overPay

Write-Host '======================================'
Write-Output 'Payroll Evaluation'
Write-Host '======================================'
Write-Output 'Employee Information'
Write-Host '--------------------------------------'
Write-Output "Full Name:       $firstName $lastName"
Write-Host "Hourly Salary:   $hSalary"
Write-Output '======================================'
Write-Host 'Time Worked Summary'
Write-Output '--------------------------------------'
Write-Host "Monday:          $monday"
Write-Output "Tuesday:         $tuesday"
Write-Host "Wednesday:       $wednesday"
Write-Output "Thursday:        $thursday"
Write-Host "Friday:          $friday"
Write-Output '======================================'
Write-Host 'Pay Summary'
Write-Output '--------------------------------------'
Write-Host 'Regular Time:  ' $regTime
Write-Output '--------------------------------------'
Write-Host 'Regular Pay:   ' $regPay
Write-Output '--------------------------------------'
Write-Host 'Over Time:     ' $overtime
Write-Output '--------------------------------------'
Write-Host 'Overtime Pay:  ' $overPay
Write-Output '======================================'
Write-Host "Net Pay:        $netPay"
Write-Output '======================================'

This would produce:

PS C:\Windows\System32> C:\Exercises\Conditions.ps1
======================================
Payroll Evaluation
======================================
Employee Information
--------------------------------------
Full Name:       Catherine Busbey
Hourly Salary:   24.37
======================================
Time Worked Summary
--------------------------------------
Monday:          10
Tuesday:         9
Wednesday:       8.5
Thursday:        10.5
Friday:          8
======================================
Pay Summary
--------------------------------------
Regular Time:   46
--------------------------------------
Regular Pay:    1121.02
--------------------------------------
Over Time:      0
--------------------------------------
Overtime Pay:   0
======================================
Net Pay:        1121.02
======================================

If you have a logical expression or value, to let you get its logical opposite, you can use an operator represented by !. The formula to use it is:

!variable-or-expression

Actually there are various ways you can use the ! logical operator. The classic way is to check whether a variable is not holding a True value. To do this, use an if() conditional statement and precede the name of the variable with the exclamation point. Here is an example:

$firstName    = "Catherine"
$lastName     = "Busbey"
$hSalary      = 24.37
$employeeIsFullTime = $false

#Time Worked"
$monday       = 10
$tuesday      = 9
$wednesday    = 8.5
$thursday     = 10.5
$friday       = 8

$timeWorked = $monday + $tuesday + $wednesday + $thursday + $friday

$regTime      = $timeWorked
$regPay       = $hSalary * $timeWorked
$overtime     = 0.00
$overPay      = 0.00

if(!$employeeIsFullTime) {
    $regTime  = 40.00
    $regPay   = $hSalary * 40.00
    $overtime = $timeWorked - 40.00
    $overPay  = $hSalary * 1.50 * $overtime
}

$netPay = $regPay + $overPay

Write-Host '======================================'
Write-Host 'Payroll Evaluation'
Write-Host '======================================'
Write-Host 'Employee Information'
Write-Host '--------------------------------------'
Write-Host "Full Name:       $firstName $lastName"
Write-Host "Hourly Salary:   $hSalary"
Write-Host '======================================'
Write-Host 'Time Worked Summary'
Write-Host '--------------------------------------'
Write-Host "Monday:          $monday"
Write-Output "Tuesday:         $tuesday"
Write-Host "Wednesday:       $wednesday"
Write-Output "Thursday:        $thursday"
Write-Host "Friday:          $friday"
Write-Host '======================================'
Write-Host 'Pay Summary'
Write-Host '--------------------------------------'
Write-Output 'Regular Time:  ' $regTime
Write-Host '--------------------------------------'
Write-Output 'Regular Pay:   ' $regPay
Write-Host '--------------------------------------'
Write-Output 'Over Time:     ' $overtime
Write-Host '--------------------------------------'
Write-Output 'Overtime Pay:  ' $overPay
Write-Host '======================================'
Write-Output "Net Pay:        $netPay"
Write-Host '======================================'

This would produce:

PS C:\Windows\System32> C:\Exercises\Conditions.ps1
======================================
Payroll Evaluation
======================================
Employee Information
--------------------------------------
Full Name:       Catherine Busbey
Hourly Salary:   24.37
======================================
Time Worked Summary
--------------------------------------
Monday:          10
Tuesday:         9
Wednesday:       8.5
Thursday:        10.5
Friday:          8
======================================
Pay Summary
--------------------------------------
Regular Time:   40
--------------------------------------
Regular Pay:    974.8000000000001
--------------------------------------
Over Time:      6
--------------------------------------
Overtime Pay:   219.32999999999998
======================================
Net Pay:        1194.13
======================================

Another way to use the exclamation point is the precede a variable with that operator and assign it to a Boolean variable.

Negating a Condition

Besides the exclamation point, another way to chec the opposite of a variable is by using an operator named -not. You use both operators practically interchangeably. Here are examples:

[string]$firstName        = "Catherine"
[string]$lastName         = "Busbey"
[float]$hSalary           = 24.37
[bool]$employeeIsFullTime = $false

#Time Worked"
[float]$monday       = 10
[float]$tuesday      = 9
[float]$wednesday    = 8.5
[float]$thursday     = 10.5
[float]$friday       = 8

$timeWorked = $monday + $tuesday + $wednesday + $thursday + $friday

[float]$regTime      = $timeWorked
[float]$regPay       = $hSalary * $timeWorked
[float]$overtime     = 0.00
[float]$overPay      = 0.00

if(-not $employeeIsFullTime) {
    $regTime  = 40.00
    $regPay   = $hSalary * 40.00
    $overtime = $timeWorked - 40.00
    $overPay  = $hSalary * 1.50 * $overtime
}

$netPay = $regPay + $overPay

Write-Output '======================================'
Write-Output 'Payroll Evaluation'
Write-Output '======================================'
Write-Output 'Employee Information'
Write-Output '--------------------------------------'
Write-Output "Full Name:       $firstName $lastName"
Write-Output "Hourly Salary:   $hSalary"
Write-Output '======================================'
Write-Output 'Time Worked Summary'
Write-Output '--------------------------------------'
Write-Output "Monday:          $monday"
Write-Output "Tuesday:         $tuesday"
Write-Output "Wednesday:       $wednesday"
Write-Output "Thursday:        $thursday"
Write-Output "Friday:          $friday"
Write-Host '======================================'
Write-Host 'Pay Summary'
Write-Host '--------------------------------------'
Write-Host 'Regular Time:  ' $regTime
Write-Host '--------------------------------------'
Write-Host 'Regular Pay:   ' $regPay
Write-Host '--------------------------------------'
Write-Host 'Over Time:     ' $overtime
Write-Host '--------------------------------------'
Write-Host 'Overtime Pay:  ' $overPay
Write-Host '======================================'
Write-Host "Net Pay:        $netPay"
Write-Host '======================================'

If you are using a conditional operator such as the if operator we have encountered so far, and you are using one of the Boolean operators we have used so far (-eq, -ne, -lt, -le, -gt, or -ge), you can first apply the -not operator on the variable. Here is an example:

$firstName    = "Catherine"
$lastName     = "Busbey"
$hSalary      = 24.37
$employeeIsFullTime = $true

#Time Worked"
$monday       = 9
$tuesday      = 8
$wednesday    = 10.5
$thursday     = 9.5
$friday       = 8.5

$timeWorked = $monday + $tuesday + $wednesday + $thursday + $friday

$regTime      = $timeWorked
$regPay       = $hSalary * $timeWorked
$overtime     = 0.00
$overPay      = 0.00

if(-not $employeeIsFullTime -eq $true) {
    $regTime  = 40.00
    $regPay   = $hSalary * 40.00
    $overtime = $timeWorked - 40.00
    $overPay  = $hSalary * 1.50 * $overtime
}

$netPay = $regPay + $overPay

Write-Host '========================================='
Write-Host 'Payroll Evaluation'
Write-Host '========================================='
Write-Host 'Employee Information'
Write-Host '-----------------------------------------'
Write-Host "Full Name:     $firstName $lastName"
Write-Host "Hourly Salary: $hSalary"
Write-Host '========================================='
Write-Host 'Time Worked Summary'
Write-Host '-----------------------------------------'
Write-Host "Monday:        $monday"
Write-Host "Tuesday:       $tuesday"
Write-Host "Wednesday:     $wednesday"
Write-Host "Thursday:      $thursday"
Write-Host "Friday:        $friday"
Write-Output '========================================='
Write-Output '    Pay Summary'
Write-Output '-----------------------------------------'
Write-Output '                     Time   Pay'
Write-Output '-----------------------------------------'
Write-Output "    Regular:         $regTime   $regPay"
Write-Output '-----------------------------------------'
Write-Output "    Overtime:        $overtime   $overPay"
Write-Output '========================================='
Write-Output "    Net Pay:         $netPay"
Write-Output '========================================='

Previous Copyright © 2024-2025, FunctionX Wednesday 11 February 2025, 15:40 Next