Scripting basics: automate the boring stuff
Why every IT pro should script, how PowerShell and bash work, and your first useful scripts, explained for people who have never written code.
You don’t need to be a programmer to work in IT, but a little scripting changes your career. Anything you do more than twice by clicking can probably be done in seconds with a script, and done the same way every time.
What a script is
A script is just a text file full of the same commands you’d type at the command line (step 2), run in order. Instead of typing ten commands, you run one file.
- PowerShell is the scripting language for Windows and Microsoft services. Windows includes Windows PowerShell 5.1, and the newer, cross-platform PowerShell 7 is a free download.
- Bash is the standard on Linux, and works on macOS too.
If you’ll mostly support Windows offices, start with PowerShell.
PowerShell in five minutes
PowerShell commands follow a Verb-Noun pattern, which makes them easy to guess:
Get-Process # list running programs
Get-Service # list services
Stop-Process -Name notepad
Restart-Service -Name Spooler # restart the print spooler
The real power is the pipeline (|), which passes results from one command into the next:
# The 5 programs using the most memory
Get-Process | Sort-Object WorkingSet -Descending | Select-Object -First 5 Name, WorkingSet
Variables hold values, and loops repeat things:
$computers = 'PC1', 'PC2', 'PC3'
foreach ($pc in $computers) {
Test-Connection -ComputerName $pc -Count 1 -Quiet
}
Your first useful script
Here’s a small script that checks free disk space and warns you when a drive is getting full, a classic cause of mysterious problems:
# Warn about any drive with less than 15% free space
Get-CimInstance Win32_LogicalDisk -Filter "DriveType=3" | ForEach-Object {
$freePercent = [math]::Round(($_.FreeSpace / $_.Size) * 100, 1)
if ($freePercent -lt 15) {
Write-Warning "$($_.DeviceID) has only $freePercent% free"
} else {
"$($_.DeviceID) is fine ($freePercent% free)"
}
}
Save it as check-disk.ps1, then run it from PowerShell with .\check-disk.ps1. If Windows blocks it, the setting that controls this is called the execution policy; Set-ExecutionPolicy RemoteSigned -Scope CurrentUser allows your own scripts to run.
Bash in two minutes
The same idea on Linux:
#!/bin/bash
# Warn about any filesystem over 85% full
df -h --output=pcent,target | tail -n +2 | while read pct mount; do
if [ "${pct%\%}" -gt 85 ]; then
echo "WARNING: $mount is $pct full"
fi
done
Save it as check-disk.sh, make it runnable with chmod +x check-disk.sh, and run it with ./check-disk.sh.
Habits that keep scripts safe
- Test in your home lab first (step 7), never on a live system.
- Read before you run. Never paste a script from the internet without understanding what each line does, especially anything with “delete,” “remove” or “format” in it.
- Start read-only. Scripts that only look at things (Get commands) can’t break anything. Add changes once you trust the output.
- Use
-WhatIfin PowerShell. Many commands support it, and it shows what would happen without doing it. - Comment your scripts (lines starting with
#), so you’ll understand them in six months.
Where to go next
- Get-Help and Get-Command are built in and excellent:
Get-Help Get-Service -Examples - Pick one task you do repeatedly and script it. Real problems teach faster than tutorials.
- There’s a collection of read-only Active Directory commands here for once you’ve built the domain from step 7.
Key takeaways
- A script is just commands in a file, run in order.
- PowerShell for Windows, bash for Linux.
- The pipeline is what makes PowerShell powerful.
- Test in a lab, read before you run, and start read-only.
From Small Town Sysadmin
Last checked October 2, 2026.
Get new guides by email
Plain-English tech tips, scam and security alerts, and fixes for small offices, whether you're the office manager or the IT pro. No spam, and you can unsubscribe anytime.