Comparison and Logical Operators
PowerShell spells comparisons as words rather than symbols: -eq, -ne, -gt, -lt, -ge, and -le for equality and ordering, plus -like and -match for wildcard and regex string comparisons, because bare symbols like < and > are reserved for file redirection in the shell. Logical combination uses -and, -or, and -not (or !), so a condition like 'status is Stopped and the service is not excluded' reads naturally as $svc.Status -eq 'Stopped' -and $svc.Name -notin $excluded.
Cricket analogy: It is like scorers using 'LBW' and 'caught behind' as specific written terms instead of ambiguous shorthand symbols, because a symbol like a slash could be misread on a busy scorecard.
# Comparison operators
5 -eq 5 # True
5 -ne 3 # True
'abc' -like 'a*' # True (wildcard)
'abc123' -match '\d+' # True (regex)
# Logical combination
$svc = Get-Service -Name 'Spooler'
if ($svc.Status -eq 'Stopped' -and $svc.Name -notin @('Excluded1')) {
Start-Service $svc
}Everyday Cmdlets Cheat Sheet
A small set of cmdlets covers most daily work: Get-ChildItem lists files/folders (alias dir, ls), Get-Content reads a file's lines, Select-Object -First N and -Property narrow output, Where-Object filters with a script block or the simplified $_.Property -op value syntax, Sort-Object orders results, and Group-Object buckets rows by a shared property — chaining them, e.g. Get-Process | Sort-Object CPU -Descending | Select-Object -First 5, is the idiomatic PowerShell equivalent of a SQL 'top N by' query. ConvertTo-Json, ConvertFrom-Json, Import-Csv, and Export-Csv round out the set for moving data in and out of scripts.
Cricket analogy: It is like a captain's go-to toolkit of five field placements that cover 90% of situations — cover, mid-on, slip, gully, fine leg — rather than needing a hundred exotic positions for every possible ball.
String Formatting and Here-Strings
Use double quotes for string interpolation ("Hello, $name") and single quotes for literal text where you don't want variables expanded; the -f format operator ('{0} used {1}% CPU' -f $name, $cpu) gives precise control over number formatting like padding or decimal places. For multi-line text — emails, config templates, SQL blocks — a here-string starting with @" on its own line and ending with "@ on its own line preserves formatting and still allows $variable interpolation inside it.
Cricket analogy: It is like the difference between a live commentator ad-libbing player names into the broadcast versus reading a fixed pre-written statement word for word — one interpolates live data, the other stays literal.
Common Gotchas Worth Memorizing
A handful of surprises trip up newcomers repeatedly: array subexpressions with a single result still behave like a scalar unless you force an array with @(), the automatic $null comparison should always put $null on the left ($null -eq $result, not $result -eq $null) to avoid a subtle bug when $result is itself a collection, and string comparisons with -eq are case-insensitive by default (use -ceq for case-sensitive). Remembering these three alone eliminates a large fraction of the 'why doesn't this work' bugs beginners hit in their first few weeks.
Cricket analogy: It is like a rookie umpire forgetting that a no-ball for overstepping is checked before checking for a wicket, not after — a small rule-ordering detail that trips up beginners repeatedly until it becomes second nature.
Keep the built-in help current with Update-Help (run elevated, once in a while) so Get-Help <cmdlet> -Examples and -Online always reflect the latest documentation instead of the version shipped when PowerShell was installed.
Writing $result -eq $null instead of $null -eq $result can silently give the wrong answer when $result is a collection, because PowerShell compares $null against every element and returns any matches as an array rather than a single boolean. Always put $null on the left.
- Comparisons use word operators (-eq, -gt, -like, -match) because <, >, and | are reserved by the shell.
- Combine conditions with -and, -or, and -not/! for readable logical expressions.
- Get-ChildItem, Where-Object, Select-Object, Sort-Object, and Group-Object cover most everyday data-shaping tasks.
- Double quotes interpolate variables; single quotes are literal; the -f operator gives precise numeric formatting.
- Here-strings (@" ... "@) preserve multi-line formatting while still allowing $variable interpolation.
- Always write $null on the left of a comparison ($null -eq $result) to avoid collection-comparison bugs.
- -eq is case-insensitive by default; use -ceq when a case-sensitive comparison is required.
Practice what you learned
1. Why does PowerShell use -eq, -gt, and -like instead of symbols like =, >, and *?
2. What is the safest way to compare a variable against $null in PowerShell?
3. What does the -f format operator do in a statement like '{0} used {1}%' -f $name, $cpu?
4. What distinguishes a here-string (@" ... "@) from a regular double-quoted string?
5. Is the -eq operator case-sensitive by default when comparing strings?
Was this page helpful?
You May Also Like
PowerShell Best Practices
Practical conventions for writing PowerShell scripts and functions that stay readable, safe, and maintainable as they grow.
PowerShell vs Bash
How PowerShell's object pipeline compares to Bash's text pipeline, and when to reach for each shell.
PowerShell Interview Questions
Common PowerShell interview topics — from the object pipeline to scoping and remoting — with the reasoning behind each answer.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics