0
0
PowerShellscripting~5 mins

Arithmetic operators in PowerShell

Choose your learning style9 modes available
Introduction

Arithmetic operators help you do math in scripts. You can add, subtract, multiply, and divide numbers easily.

Calculate total price from item costs.
Find the difference between two dates in days.
Multiply quantities to get a total amount.
Divide a number to find an average.
Increase a value by a certain percentage.
Syntax
PowerShell
$result = <number1> <operator> <number2>

Operators include +, -, *, /, % for addition, subtraction, multiplication, division, and remainder.

Use parentheses () to control the order of operations.

Examples
Adds 5 and 3, subtracts 4 from 10.
PowerShell
$sum = 5 + 3
$diff = 10 - 4
Multiplies 7 by 6, divides 20 by 4.
PowerShell
$product = 7 * 6
$quotient = 20 / 4
Finds the remainder when 10 is divided by 3.
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"
OutputSuccess
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.