Arithmetic operators help you do math in scripts. You can add, subtract, multiply, and divide numbers easily.
Arithmetic operators in PowerShell
Introduction
Syntax
PowerShell
$result = <number1> <operator> <number2>
Operators include +, -, *, /, % for addition, subtraction, multiplication, division, and remainder.
Use parentheses () to control the order of operations.
Examples
PowerShell
$sum = 5 + 3 $diff = 10 - 4
PowerShell
$product = 7 * 6 $quotient = 20 / 4
PowerShell
$remainder = 10 % 3
Sample Program
This script shows all basic arithmetic operations between two numbers.
PowerShell
$a = 15 $b = 4 $sum = $a + $b $diff = $a - $b $product = $a * $b $quotient = $a / $b $remainder = $a % $b Write-Output "Sum: $sum" Write-Output "Difference: $diff" Write-Output "Product: $product" Write-Output "Quotient: $quotient" Write-Output "Remainder: $remainder"
Important Notes
Division (/) returns a decimal number if needed.
The remainder operator (%) gives the leftover part after division.
Use variables to store numbers and results for easy reuse.
Summary
Arithmetic operators let you do math in PowerShell scripts.
Use +, -, *, /, and % for common math tasks.
Store results in variables and print them with Write-Output.
Practice
1. Which arithmetic operator in PowerShell is used to find the remainder of a division?
easy
Solution
Step 1: Understand the modulus operator
The modulus operator (%) returns the remainder after division of two numbers.Step 2: Compare with other operators
Division (/) returns the quotient, multiplication (*) multiplies, subtraction (-) subtracts.Final Answer:
The modulus operator (%) -> Option CQuick Check:
Remainder = % operator [OK]
Hint: Remainder uses % operator in math [OK]
Common Mistakes:
- Confusing division (/) with modulus (%)
- Using * or - for remainder
- Thinking % means percentage, not remainder
2. Which of the following is the correct syntax to multiply two numbers 5 and 3 in PowerShell?
easy
Solution
Step 1: Identify the multiplication operator in PowerShell
PowerShell uses * for multiplication between numbers.Step 2: Check syntax correctness
Write-Output 5 * 3 correctly multiplies 5 and 3 and outputs the result.Final Answer:
Write-Output 5 * 3 -> Option BQuick Check:
Multiplication uses * operator [OK]
Hint: Use * for multiplication in PowerShell [OK]
Common Mistakes:
- Using 'x' or 'multiply' instead of *
- Using & which is a different operator
- Missing spaces around operator causing errors
3. What will be the output of the following PowerShell code?
$a = 10
$b = 3
$c = $a / $b
Write-Output $c
medium
Solution
Step 1: Understand division operator behavior
In PowerShell, dividing integers returns a floating-point number with decimals.Step 2: Calculate 10 divided by 3
10 / 3 equals approximately 3.33333333333333.Final Answer:
3.33333333333333 -> Option DQuick Check:
Division returns float result [OK]
Hint: Division returns decimal numbers, not just integers [OK]
Common Mistakes:
- Expecting integer 3 instead of decimal
- Thinking division causes error
- Confusing with integer division truncation
4. Identify the error in this PowerShell script:
$x = 8
$y = 2
$z = $x % $y
Write-Output $z
medium
Solution
Step 1: Check validity of modulus operator
PowerShell supports % as the modulus operator to get remainder.Step 2: Verify variable usage and output
Variables can be assigned without explicit types, and Write-Output prints values correctly.Final Answer:
There is no error; script runs correctly -> Option AQuick Check:
Modulus and Write-Output are valid [OK]
Hint: Modulus (%) works fine; no type declaration needed [OK]
Common Mistakes:
- Thinking % is invalid operator
- Believing variables need explicit types
- Assuming Write-Output can't print variables
5. You want to calculate the average of three numbers 4, 7, and 9 in PowerShell. Which script correctly does this using arithmetic operators?
hard
Solution
Step 1: Understand average calculation formula
Average = sum of numbers divided by count, so (4 + 7 + 9) / 3.Step 2: Check each option's calculation
$avg = (4 + 7 + 9) / 3; Write-Output $avg correctly sums all three then divides by 3. $avg = 4 + 7 + 9 / 3; Write-Output $avg divides 9 by 3 first, then adds 4 and 7, wrong order. $avg = 4 + 7 + 9 % 3; Write-Output $avg uses modulus incorrectly. $avg = (4 * 7 * 9) / 3; Write-Output $avg multiplies numbers, not sum.Final Answer:
$avg = (4 + 7 + 9) / 3; Write-Output $avg -> Option AQuick Check:
Average = sum / count [OK]
Hint: Use parentheses to sum before dividing for average [OK]
Common Mistakes:
- Missing parentheses causing wrong order
- Using modulus (%) instead of division
- Multiplying numbers instead of summing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
