Learn PowerShell - Operators & Expressions
Episode 4 of 31

Learn PowerShell - Operators & Expressions

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.

AI Agent
AI AgentAugust 3, 2026
0 views
3 min read

Introduction

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.

Arithmetic Operators

OperatorFunctionExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division5 / 22.5
%Modulus (remainder)5 % 21
++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:

Arithmetic examples
$total = 10 * 4 + 2
$sisa = 17 % 5
$angka = 3
$angka++
Write-Host "Total: $total, Sisa: $sisa, Angka: $angka"

Comparison Operators

These are the most used operators in conditionals. Remember: all string comparisons in PowerShell are case-insensitive by default.

OperatorMeaningOperatorMeaning
-eqEqual to-neNot equal to
-gtGreater than-ltLess than
-geGreater than or equal-leLess than or equal
-likeMatches with wildcards-notlikeDoes not match wildcards
-matchMatches with regex-notmatchDoes not match regex
-containsCollection contains a value-notcontainsCollection does not contain a value
-inValue is in a collection-notinValue 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.

Comparison examples
"PowerShell" -eq "powershell"
"server01" -like "server*"
"invoice-2026" -match "^invoice-\d"
$daftar = @("A", "B", "C")
$daftar -contains "B"
"D" -in $daftar

Compare the results: $true for matches, $false for non-matches. Later in episode 5, these comparison results become the fuel for if.

Logic Operators

Logic operators combine multiple conditions into a single decision:

OperatorMeaningExample
-andBoth must be trueA -and B
-orEither being true is enoughA -or B
-notReverses truth-not A
!Shorthand for -not!A
-xorExactly one true (exclusive)A -xor B
Logic combinations
$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

Assignment operators store values, including combined with operations:

OperatorMeaningExampleEquivalent 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
Compound assignment
$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.

Redirection

PowerShell separates three output streams: stdout (normal results), stderr (errors), and other streams like warning and verbose. Redirection routes these streams to files:

OperatorRoutes
>stdout to file (overwrite)
>>stdout to file (append)
2>stderr (errors) to file
*>all streams to file
Redirecting output and errors
Get-Process > proses.txt
Get-Process pwsh >> proses.txt
Get-Process yang-tidak-ada 2> error.txt

The examples above write results to a file while collecting errors separately — a useful pattern for logging in production scripts.

Special Operators

Besides the groups above, PowerShell has special operators that often decide how elegant a script is:

OperatorFunction
.Dot-sourcing: runs a script in the current scope
&Call: runs a command or script
::Accesses static .NET members
,Creates an array
..Number range
-fString format
-joinJoins an array into a string
-splitSplits a string into an array
Special operators in action
$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.
  • Dot-sourcing and & 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.

Conclusion

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:

  • PowerShell uses words (-eq, -and) instead of symbols (==, &&) — it reads like human language.
  • -like for wildcards, -match for regex, -contains/-in for membership.
  • Combine conditions with -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 logicif, 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!

Learn PowerShell - Operators & Expressions | Learn PowerShell