Learn PowerShell - 10 Code Examples & CST Typing Practice Test
PowerShell is a cross-platform task automation and configuration management framework, combining a command-line shell, scripting language, and object-based pipeline built on .NET. It is widely used for DevOps, system administration, cloud automation, and enterprise workflows.
View all 10 PowerShell code examples →
Learn POWERSHELL with Real Code Examples
Updated Nov 19, 2025
Code Sample Descriptions
PowerShell Counter and Theme Toggle
$count = 0
$isDark = $false
function Update-UI {
Write-Host "Counter: $count"
Write-Host "Theme: $(if ($isDark) { 'Dark' } else { 'Light' })"
}
function Increment { $global:count += 1; Update-UI }
function Decrement { $global:count -= 1; Update-UI }
function Reset { $global:count = 0; Update-UI }
function Toggle-Theme { $global:isDark = -not $global:isDark; Update-UI }
# Simulate actions
Update-UI
Increment
Increment
Toggle-Theme
Decrement
Reset
Demonstrates a simple counter with theme toggling using PowerShell variables and console output.
PowerShell Simple Calculator
function Add($a, $b) { $a + $b }
function Subtract($a, $b) { $a - $b }
function Multiply($a, $b) { $a * $b }
function Divide($a, $b) { $a / $b }
Write-Host "Add 5 + 3: $(Add 5 3)"
Write-Host "Subtract 5 - 3: $(Subtract 5 3)"
Write-Host "Multiply 5 * 3: $(Multiply 5 3)"
Write-Host "Divide 6 / 2: $(Divide 6 2)"
Performs basic arithmetic operations using functions.
PowerShell Factorial
function Factorial($n) {
if ($n -le 1) { return 1 }
else { return $n * (Factorial ($n - 1)) }
}
Write-Host "Factorial of 5: $(Factorial 5)"
Recursive factorial function.
PowerShell Fibonacci Sequence
function Fibonacci($n) {
if ($n -eq 0) { return 0 }
elseif ($n -eq 1) { return 1 }
else { return (Fibonacci ($n - 1)) + (Fibonacci ($n - 2)) }
}
0..9 | ForEach-Object { Write-Host (Fibonacci $_) }
Generate Fibonacci numbers using recursion.
PowerShell Array Comprehension
$numbers = 1..10
$squares = $numbers | Where-Object { $_ % 2 -eq 0 } | ForEach-Object { $_ * $_ }
Write-Host $squares
Square even numbers using array operations.
PowerShell Hashtable Filtering
$scores = @{ Alice=10; Bob=5; Charlie=12 }
$highScores = $scores.GetEnumerator() | Where-Object { $_.Value -ge 10 } | ForEach-Object { $_.Name, $_.Value }
Write-Host $highScores
Filtering hashtable based on values.
PowerShell Anonymous Functions
$add = { param($x,$y) $x + $y }
Write-Host ($add.Invoke(3,7))
Using scriptblocks for anonymous functions.
PowerShell Sum of Array
$numbers = 1,2,3,4,5
$sum = 0
foreach ($n in $numbers) { $sum += $n }
Write-Host $sum
Sum elements of an array.
PowerShell Zip Arrays
$arr1 = 1,2,3
$arr2 = 4,5,6
for ($i=0; $i -lt $arr1.Length; $i++) { Write-Host ($arr1[$i] + $arr2[$i]) }
Combine two arrays element-wise.
PowerShell String Manipulation
$str = 'HelloWorld'
$sub = $str.Substring(0,5)
$len = $str.Length
Write-Host "Substring: $sub, Length: $len"
Substring extraction and length.
Frequently Asked Questions about PowerShell
What is PowerShell?
PowerShell is a cross-platform task automation and configuration management framework, combining a command-line shell, scripting language, and object-based pipeline built on .NET. It is widely used for DevOps, system administration, cloud automation, and enterprise workflows.
What are the primary use cases for PowerShell?
System administration & configuration. Cloud automation (Azure & Microsoft 365). DevOps & CI/CD pipelines. Remote management & orchestration. File management & process automation. Infrastructure as Code (IaC) scripting
What are the strengths of PowerShell?
Object pipeline avoids string parsing. Deep integration with Windows & Azure. Enterprise-grade automation. Extensible with .NET. Powerful remoting & orchestration
What are the limitations of PowerShell?
Slower startup vs shells like Bash. Learning curve due to object model. Platform differences between Windows & Linux. Heavy for simple one-liners. Verbose syntax compared to POSIX shells
How can I practice PowerShell typing speed?
CodeSpeedTest offers 10+ real PowerShell code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.