5.1.8. Explain the basics of scripting. (Obj. 4.8)
💡 First Principle: Scripting automates repetitive tasks, improving efficiency and reducing the potential for human error. If you do something more than twice, script it.
At its core, a script is just a simple computer program written as a plain text file. It contains a series of commands that are executed in sequence by an interpreter. The power of scripting is automation. If you find yourself performing the same set of command-line tasks over and over again, you can put those commands into a script and then run the script with a single command. This saves time, ensures the task is performed exactly the same way every time (reducing errors), and allows non-technical users to perform complex tasks.
Script Types Quick Reference:
| Type | Extension | Platform | Use Case | Security Note |
|---|---|---|---|---|
| Batch | .bat | Windows | Simple automation, drive mapping | Runs immediately on double-click |
| PowerShell | .ps1 | Windows | Advanced admin, system management | Execution policy blocks by default |
| VBScript | .vbs | Windows | Legacy admin and logon scripts (Windows Script Host) | Deprecated by Microsoft; a common malware carrier |
| Shell | .sh | Linux/macOS | System administration, automation | Requires execute permission (chmod +x) |
| Python | .py | Cross-platform | Data processing, complex logic | Requires Python interpreter |
| JavaScript | .js | Web/Node.js | Web automation, server-side tasks | Browser sandboxed; Node.js unrestricted |
Figure: PowerShell 7.5 running in Windows Terminal - the modern automation framework for Windows administration
Deep Dive: Script Language Characteristics
-
Batch files (
.bat): The original scripting language for Windows. Uses DOS-style commands likecopy,del,echo. Simple but limited - no objects, weak error handling. Still useful for quick tasks. -
PowerShell (
.ps1): The modern Windows automation framework. Key insight: PowerShell works with objects, not text. When you runGet-Process, you get process objects with properties like.CPUand.Memory, not just lines of text. This makes it incredibly powerful for system administration.Critical Security Feature: By default, PowerShell will NOT run scripts (execution policy is
Restricted). You must explicitly change this usingSet-ExecutionPolicy. Common policies:Restricted- No scripts run (default)RemoteSigned- Downloaded scripts must be signed; local scripts run freelyUnrestricted- All scripts run (dangerous)
-
Shell script (
.sh): The standard for Linux and macOS. Runs in Bash or other shells. The first line (#!/bin/bash) tells the system which interpreter to use - this is called the shebang. -
Python (
.py): Readable, powerful, cross-platform. Excellent for complex logic, data manipulation, and when you need libraries (e.g., for working with APIs, Excel files, or databases). -
JavaScript (
.js): Primarily for web browsers, but Node.js allows server-side execution. Used for web automation and modern tooling. -
Comments: Lines the interpreter ignores, used to explain what the code does. PowerShell, shell scripts, and Python use
#; batch files useREM(or::); VBScript uses'(an apostrophe); JavaScript uses//.
Practical Examples:
Batch File - Map Network Drives:
@echo off
REM Map sales department drives
net use S: \\FILESRV01\Sales /persistent:yes
net use T: \\FILESRV01\TeamDocs /persistent:yes
net use P: \\FILESRV01\Public /persistent:yes
echo All drives mapped successfully!
pause
PowerShell - System Health Check:
# Get disk space on C: drive (objects, not text!)
$disk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'"
$freeGB = [math]::Round($disk.FreeSpace / 1GB, 2)
$totalGB = [math]::Round($disk.Size / 1GB, 2)
$percentFree = [math]::Round(($disk.FreeSpace / $disk.Size) * 100, 1)
Write-Host "C: Drive: $freeGB GB free of $totalGB GB ($percentFree% free)"
if ($percentFree -lt 10) {
Write-Host "WARNING: Low disk space!" -ForegroundColor Red
}
Shell Script (Linux) - Clear Temp Files:
#!/bin/bash
# Clear temporary files older than 7 days
echo "Cleaning temp files..."
find /tmp -type f -mtime +7 -delete
echo "Done! Cleaned files older than 7 days."
Common Use Cases for Help Desk:
- User onboarding: Map drives, install standard software, configure printers
- Maintenance: Clear temp files, check disk space, restart services
- Reporting: Gather system info, check for updates, audit installed software
- Troubleshooting: Reset network settings, clear caches, repair common issues
The objectives name these use cases: basic automation of routine tasks, restarting machines, remapping network drives, installation of applications, automated backups, gathering of information/data, and initiating updates.
Script risks: A script has no judgment: it repeats any mistake on every machine it runs on, so test it on a non-production system before deploying it. Watch for unintentionally introducing malware (running scripts from untrusted sources), inadvertently changing system settings or deleting files through an untested or mistyped command, and browser or system crashes due to mishandling of resources (for example, a loop that never releases memory).
Technician's Insight: You don't need to be a programmer, but you should be able to:
- Identify script types by extension
- Understand what a script is doing at a high level
- Modify existing scripts (change a path, add a command)
- Know the security implications (especially PowerShell execution policy)
Reflection Question: A user double-clicks a .ps1 file and nothing happens. What is the most likely cause, and how would you enable them to run it safely?