Skip to content

PowerShell Arithmetic Operators

How PowerShell arithmetic operators (+, -, *, /, %) work with numbers, strings, arrays, and built-in multipliers (KB, MB, GB) for calculations, type conversion, and automation tasks.

Arithmetic operators calculate mathematical values, combine text strings, replicate collections, and determine division remainders. Unlike traditional shells like Windows Command Prompt (which requires set /a and is restricted to 32-bit integers), PowerShell natively handles floating-point numbers, automatically widens numeric types to prevent overflow, and overloads arithmetic symbols to manipulate strings and arrays.

PowerShell includes five core arithmetic operators: addition, subtraction, multiplication, division, and modulus (remainder).

Core Arithmetic Operators

Operator Description PowerShell Syntax Result
+ Addition / Concatenation 15 + 5 20
- Subtraction 25 - 8 17
* Multiplication / Replication 6 * 7 42
/ Division 40 / 8 5
% Modulo (Remainder) 17 % 5 2
arithmetic-basics.ps1powershell
$a = 15
$b = 4

Write-Output ($a + $b)
Write-Output ($a - $b)
Write-Output ($a * $b)
Write-Output ($a / $b)
Write-Output ($a % $b)
Consolepowershell
./arithmetic-basics.ps1
Outputplaintext
19
11
60
3.75
3

where:

  • / always performs floating-point division by default, even when dividing two integers. Note the result 3.75 above, not a truncated integer.
  • % returns the remainder left over after dividing $a by $b.

Operator Overloading and Type Behaviors

In PowerShell, arithmetic operators behave differently depending on the data type of the left-hand operand. The same symbol (+, *) performs different operations based on the data type of its operands: numeric addition for numbers, string concatenation, and array combination.

Addition (+)

  • Numbers: Performs numeric addition.
  • Strings: Concatenates strings together.
  • Arrays: Merges collections into a new, combined array.
Consolepowershell
10 + 25

"Power" + "Shell"

@(1, 2) + @(3, 4)
Outputplaintext
35
PowerShell
1
2
3
4

Subtraction (-)

Subtracts numbers or calculates time differences between dates:

Consolepowershell
100 - 35

(Get-Date "2026-01-01") - (Get-Date "2026-12-25")
Outputplaintext
65

Days              : -358
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 0
Ticks             : -309312000000000
TotalDays         : -358
TotalHours        : -8592
TotalMinutes      : -515520
TotalSeconds      : -30931200
TotalMilliseconds : -30931200000

Multiplication (*)

  • Numbers: Multiplies values.
  • String Replication: Repeats a string a specified number of times.
  • Array Replication: Duplicates array elements into a new array.
Consolepowershell
8 * 5

"-" * 20

@("NodeA", "NodeB") * 2
Outputplaintext
40
--------------------
NodeA
NodeB
NodeA
NodeB

Division (/)

Divides two numbers. Unlike languages like C# or Python 2 that truncate non-divisible integers, PowerShell automatically converts integer division to a System.Double if there is a remainder:

Consolepowershell
10 / 2

7 / 2
(7 / 2).GetType().Name
Outputplaintext
5
3.5
Double

Modulo (%)

Calculates the integer remainder left over after dividing the left operand by the right operand. Modulo is frequently used in automation for running tasks at specific intervals, formatting alternating rows, or distributing workloads across worker pools:

Consolepowershell
10 % 3
15 % 5
Outputplaintext
1
0

Operator Precedence

Operator precedence defines the sequence in which PowerShell calculates compound expressions. Multiplicative operators are evaluated before additive operators:

# Operator Description
1 Parentheses () [Highest Precedence]
2 Unary Operators -, !, ++, --
3 Multiplicative Operators *, /, %
4 Additive Operators +, - [Lowest Precedence]

When two operators share the same precedence tier (such as * and /), PowerShell evaluates them from left to right:

precedence-rules.ps1powershell
$resultA = 10 + 5 * 2

$resultB = (10 + 5) * 2

$resultC = 100 / 5 * 2

[PSCustomObject]@{
    ExpressionA = "10 + 5 * 2"
    ResultA     = $resultA
    ExpressionB = "(10 + 5) * 2"
    ResultB     = $resultB
    ExpressionC = "100 / 5 * 2"
    ResultC     = $resultC
}
Consolepowershell
pwsh ./precedence-rules.ps1
Outputplaintext
ExpressionA : 10 + 5 * 2
ResultA     : 20
ExpressionB : (10 + 5) * 2
ResultB     : 30
ExpressionC : 100 / 5 * 2
ResultC     : 40

Numeric Multipliers

PowerShell features built-in size multiplier suffixes that simplify byte calculations in system administration tasks:

Suffix Multiplier Exact Decimal Bytes
KB Kilobyte ($1024$) $1,024$
MB Megabyte ($1024^2$) $1,048,576$
GB Gigabyte ($1024^3$) $1,073,741,824$
TB Terabyte ($1024^4$) $1,099,511,627,776$
PB Petabyte ($1024^5$) $1,125,899,906,842,624$

These multipliers can be combined directly with arithmetic operators:

Consolepowershell
50GB

10GB / 500MB
Outputplaintext
53687091200
20.48

Practical Example: Workload Chunking and Capacity Planning

The following script uses arithmetic operators (-, /, *, %) to inspect disk capacity, calculate utilization percentages, and distribute batch processing jobs across parallel worker queues:

compute-capacity-and-workers.ps1powershell
param (
    [int]$TotalJobs   = 23,
    [int]$WorkerCount = 4
)

$Drive = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceID='C:'"
$TotalBytes = $Drive.Size
$FreeBytes  = $Drive.FreeSpace

$UsedBytes   = $TotalBytes - $FreeBytes
$PercentUsed = ($UsedBytes / $TotalBytes) * 100

$JobsPerWorker = [math]::Floor($TotalJobs / $WorkerCount)
$RemainingJobs = $TotalJobs % $WorkerCount

[PSCustomObject]@{
    DriveTested     = $Drive.DeviceID
    TotalGB         = [math]::Round($TotalBytes / 1GB, 2)
    UsedGB          = [math]::Round($UsedBytes / 1GB, 2)
    PercentUsed     = [math]::Round($PercentUsed, 1)
    TotalBatchJobs  = $TotalJobs
    AssignedWorkers = $WorkerCount
    BaseJobsPerNode = $JobsPerWorker
    LeftoverJobs    = $RemainingJobs
}

where:

  • $TotalBytes - $FreeBytes uses subtraction to calculate consumed storage.
  • ($UsedBytes / $TotalBytes) * 100 uses division and multiplication to calculate capacity percentage.
  • 1GB is the built-in numeric multiplier for byte conversion.
  • [math]::Floor($TotalJobs / $WorkerCount) divides jobs evenly across nodes.
  • $TotalJobs % $WorkerCount uses modulo to determine how many remaining jobs must be assigned to worker overflow pools.

Execute the script from the console:

Consolepowershell
pwsh ./compute-capacity-and-workers.ps1 -TotalJobs 23 -WorkerCount 4
Outputplaintext
DriveTested     : C:
TotalGB         : 255.4
UsedGB          : 213.25
PercentUsed     : 83.5
TotalBatchJobs  : 23
AssignedWorkers : 4
BaseJobsPerNode : 5
LeftoverJobs    : 3
Navigation