Mastering operators and expressions in PowerShell: arithmetic with division and modulus, word-based comparisons like -eq, -like, and -match, the -and, -or, and -not logic operators, assignment, redirection to files, plus special operators like dot-sourcing, call, range, format, -join, and -split used in almost every script.

In episode 3 you mastered how to store data — variables and types. Episode 4 completes it with how to process data: operators and expressions. If variables are boxes holding values, operators are the engines that read those values and produce new ones — adding, comparing, combining, and deciding.
One cultural difference to remember from the start: most languages use symbols (==, !=, &&), while PowerShell uses words (-eq, -ne, -and). Try running 5 -gt 3 in your console — the result is $true, and notice how readable the words are, like a sentence. It feels foreign at first, but this is exactly what makes PowerShell scripts read like human language — and later it becomes part of its identity.
| Operator | Function | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 5 / 2 | 2.5 |
% | Modulus (remainder) | 5 % 2 | 1 |
++ | Increment by one | $i++ | increments $i |
-- | Decrement by one | $i-- | decrements $i |
Note the division: 5 / 2 gives 2.5 (not 2) because PowerShell divides according to the data types involved. % (modulus) is very useful — for example determining even/odd or processing every Nth item in a loop:
$total = 10 * 4 + 2
$sisa = 17 % 5
$angka = 3
$angka++
Write-Host "Total: $total, Sisa: $sisa, Angka: $angka"These are the most used operators in conditionals. Remember: all string comparisons in PowerShell are case-insensitive by default.
| Operator | Meaning | Operator | Meaning |
|---|---|---|---|
-eq | Equal to | -ne | Not equal to |
-gt | Greater than | -lt | Less than |
-ge | Greater than or equal | -le | Less than or equal |
-like | Matches with wildcards | -notlike | Does not match wildcards |
-match | Matches with regex | -notmatch | Does not match regex |
-contains | Collection contains a value | -notcontains | Collection does not contain a value |
-in | Value is in a collection | -notin | Value is not in a collection |
-like uses wildcards (*, ?), while -match uses regular expressions — a more powerful pattern. -contains and -in check membership, with reversed positions: -contains reads "collection contains a value", -in reads "value is in a collection". Remember these pairs, because in episode 5 they become the fuel for if.
"PowerShell" -eq "powershell"
"server01" -like "server*"
"invoice-2026" -match "^invoice-\d"
$daftar = @("A", "B", "C")
$daftar -contains "B"
"D" -in $daftarCompare the results: $true for matches, $false for non-matches. Later in episode 5, these comparison results become the fuel for if.
Logic operators combine multiple conditions into a single decision:
| Operator | Meaning | Example |
|---|---|---|
-and | Both must be true | A -and B |
-or | Either being true is enough | A -or B |
-not | Reverses truth | -not A |
! | Shorthand for -not | !A |
-xor | Exactly one true (exclusive) | A -xor B |
$usia = 25
$punyaSIM = $true
$bisaMenyetir = ($usia -ge 17) -and $punyaSIM
$bukanAnakAnak = -not ($usia -lt 17)
Write-Host "Bisa menyetir: $bisaMenyetir"
Write-Host "Bukan anak-anak: $bukanAnakAnak"Use parentheses to make evaluation order explicit — this keeps a script's intent clearly readable and prevents surprises.
Assignment operators store values, including combined with operations:
| Operator | Meaning | Example | Equivalent To |
|---|---|---|---|
= | Assigns a value | $x = 5 | $x = 5 |
+= | Add then store | $x += 2 | $x = $x + 2 |
-= | Subtract then store | $x -= 2 | $x = $x - 2 |
*= | Multiply then store | $x *= 2 | $x = $x * 2 |
/= | Divide then store | $x /= 2 | $x = $x / 2 |
%= | Modulus then store | $x %= 2 | $x = $x % 2 |
$skor = 0
$skor += 10
$skor += 5
$skor *= 2
Write-Host "Skor akhir: $skor"+= is also used to add elements to an array, as you saw in episode 3.
PowerShell separates three output streams: stdout (normal results), stderr (errors), and other streams like warning and verbose. Redirection routes these streams to files:
| Operator | Routes |
|---|---|
> | stdout to file (overwrite) |
>> | stdout to file (append) |
2> | stderr (errors) to file |
*> | all streams to file |
Get-Process > proses.txt
Get-Process pwsh >> proses.txt
Get-Process yang-tidak-ada 2> error.txtThe examples above write results to a file while collecting errors separately — a useful pattern for logging in production scripts.
Besides the groups above, PowerShell has special operators that often decide how elegant a script is:
| Operator | Function |
|---|---|
. | Dot-sourcing: runs a script in the current scope |
& | Call: runs a command or script |
:: | Accesses static .NET members |
, | Creates an array |
.. | Number range |
-f | String format |
-join | Joins an array into a string |
-split | Splits a string into an array |
$rentang = 1..5
$daftar = ,"satu"
$kata = "api-key-2026"
$bagian = $kata -split "-"
$gabung = $bagian -join "/"
[math]::Pow(2, 10)1..5 produces the array 1 2 3 4 5 — the basis of loops later.-split splits a string into an array; -join does the opposite.:: calls static methods — [math]::Pow(2, 10) computes 2 to the power of 10.& will be covered in depth in the functions and script execution episode.Tip
The -split and -join operators are a pair that reverse each other: "a-b-c" -split "-" gives a b c, and @("a","b","c") -join "/" turns it back into a/b/c. Master both, and text processing in your scripts will feel much cleaner.
Episode 4 completes the "engine" for data processing: arithmetic with division and modulus, word-based comparisons from -eq to -in, the -and, -or, and -not logic, compound assignment, output redirection for logging, and the special operators that give extra power — range, split-join, format, and static members.
Key takeaways:
-eq, -and) instead of symbols (==, &&) — it reads like human language.-like for wildcards, -match for regex, -contains/-in for membership.-and/-or and use parentheses for clarity.> and 2> separate normal results from errors — the foundation of logging.1..5, -split, -join, and :: are small tools with a big impact.In the next episode, episode 5, we use all these operators for the most practical thing: conditional logic — if, else, and switch — how scripts make decisions based on system state, plus the modern ternary and null-coalescing features in PowerShell 7. See you in episode 5!