PowerShell Basics

Beginner-friendly, W3Schools-style walkthrough: short lesson, quick example, copy, run, repeat. Start at Lesson 1 and work down the page.

Lesson 1: Write output

Use Write-Output to print text or values.

Write-Output "Hello, PowerShell"
Write-Output "Welcome to Lesson 1"
Copied!

Lesson 2: Variables

Variables start with $. Assign with =.

$name = "Frank"
$score = 95

Write-Output $name
Write-Output "Score: $score"
Copied!

Lesson 3: Arrays

Create arrays with commas or @(...).

$colors = "red", "blue", "green"
$characters = @("Leon Kennedy", "Jill Valentine", "Chris Redfield", "Claire Redfield")

Write-Output $colors
Write-Output $characters
Write-Output $characters.Count
Copied!

Lesson 4: Array indexes

Index starts at 0. Negative index counts from the end.

$numbers = 10, 20, 30, 40, 50

$numbers[0]
$numbers[2]
$numbers[-1]
$numbers[1..3]
Copied!

Lesson 5: Ranges

Use .. to create ranges fast.

1..10
1..25

$ids = 100..105
Write-Output $ids
Copied!

Lesson 6: Random item selection

Pick one or many random values with Get-Random.

$characters = @("Leon Kennedy", "Jill Valentine", "Chris Redfield", "Claire Redfield")

    $characters | Get-Random
    $characters | Get-Random -Count 2
    $characters | Get-Random -Shuffle
Copied!

Tip: If output repeats in testing, check whether -SetSeed was used.

Lesson 7: Loops

Use foreach to loop values and for for index-based loops.

foreach ($number in 1..25) {
    Write-Output $number
}
Copied!
$characters = @("Leon Kennedy", "Jill Valentine", "Chris Redfield", "Claire Redfield")

for ($i = 0; $i -lt $characters.Count; $i++) {
    Write-Output "$i : $($characters[$i])"
}
Copied!

Lesson 7.5: Check if a port is open

Use Test-NetConnection with -Port to verify service reachability.

Test-NetConnection google.com -Port 443
Test-NetConnection 8.8.8.8 -Port 53
Test-NetConnection localhost -Port 80
Copied!

Look at TcpTestSucceeded. True means the target responded on that port.

Lesson 8: Files and folders

These are daily-use file commands.

Get-Location
Get-ChildItem
Get-ChildItem -Recurse
Set-Location .\folder-name
New-Item -ItemType Directory .\reports
Copy-Item .\report.txt .\reports\report.txt
Copied!

Lesson 9: Pipeline and filtering

Pipe output using |, then filter/sort/select.

Get-ChildItem -Recurse | Where-Object Length -gt 1MB
Get-ChildItem -Recurse | Sort-Object Length -Descending | Select-Object Name, Length -First 10
Get-Process | Where-Object CPU -gt 100 | Select-Object ProcessName, CPU
Copied!

Lesson 10: Help and practical IT commands

Get-Help Get-ChildItem -Examples
Get-Command *process*
Get-FileHash .\example.zip
Get-FileHash .\example.zip -Algorithm SHA256
Test-NetConnection google.com
Get-NetIPConfiguration
Get-Process
Stop-Process -Name notepad
Copied!

Use Stop-Process carefully because it can close apps and lose unsaved work.

Advanced: Check one port across many servers

Use an IP list and loop through each server to test the same port.

$servers = @(
    "192.168.1.10",
    "192.168.1.11",
    "192.168.1.12"
)

$port = 443

foreach ($server in $servers) {
    $result = Test-NetConnection $server -Port $port

    [PSCustomObject]@{
        Server = $server
        Port = $port
        Open = $result.TcpTestSucceeded
    }
}
Copied!
$servers = @("192.168.1.10", "192.168.1.11", "192.168.1.12")
$port = 443

$results = foreach ($server in $servers) {
    $result = Test-NetConnection $server -Port $port
    [PSCustomObject]@{
        Server = $server
        Port = $port
        Open = $result.TcpTestSucceeded
    }
}

$results | Format-Table -AutoSize
Copied!

Advanced: Check multiple ports per server (matrix output)

Use nested loops to test each server against each port, then show a table-style matrix.

$servers = @("192.168.1.10", "192.168.1.11", "192.168.1.12")
$ports = @(80, 443, 3389)

$matrix = foreach ($server in $servers) {
    $portResults = @{}

    foreach ($port in $ports) {
        $test = Test-NetConnection $server -Port $port
        $portResults["Port$port"] = $test.TcpTestSucceeded
    }

    [PSCustomObject]@{
        Server = $server
        Port80 = $portResults["Port80"]
        Port443 = $portResults["Port443"]
        Port3389 = $portResults["Port3389"]
    }
}

$matrix | Format-Table -AutoSize
Copied!
$servers = @("192.168.1.10", "192.168.1.11", "192.168.1.12")
$ports = @(80, 443, 3389)

$matrix = foreach ($server in $servers) {
    $checks = foreach ($port in $ports) {
        $result = Test-NetConnection $server -Port $port
        [PSCustomObject]@{
            Port = $port
            Open = $result.TcpTestSucceeded
        }
    }

    [PSCustomObject]@{
        Server = $server
        Port80 = ($checks | Where-Object Port -eq 80).Open
        Port443 = ($checks | Where-Object Port -eq 443).Open
        Port3389 = ($checks | Where-Object Port -eq 3389).Open
    }
}

$matrix | Export-Csv .\port-check-matrix.csv -NoTypeInformation
$matrix | Format-Table -AutoSize
Copied!

Replace the IP list and port list for your environment. True means the port responded.

Quick reference table

Task Command
List files Get-ChildItem
List files recursively Get-ChildItem -Recurse
Create array $items = @("a", "b", "c")
Pick random item $items | Get-Random
Loop a range foreach ($n in 1..25) { Write-Output $n }
Get help examples Get-Help CommandName -Examples

Optional background

Why IT pros and coders use PowerShell
  • Automate repetitive tasks and reduce manual click work.
  • Use object-based output for cleaner filtering and reporting.
  • Handle practical jobs quickly: hashes, connectivity checks, process management, and file audits.

Related pages

Vim, Git Cheatsheet, and Curl

Reference sources used