Scripting and Automation

Scripting and Automation: Python, PowerShell, and Bash

Python, PowerShell, and Bash are the three most useful scripting languages for IT professionals. Bash is the native shell language on Linux and macOS. PowerShell is the primary automation tool for Windows environments. Python works across all platforms and is the most broadly applicable for complex logic, API integration, and data processing. Most experienced technicians know at least two of the three.

Bash scripting for Linux and macOS automation

Bash is the default shell on most Linux distributions and macOS. Bash scripts automate tasks that would otherwise require typing sequences of commands manually: backups, log rotation, user provisioning, software installation, and scheduled maintenance. The language is close to the interactive shell, so command-line knowledge transfers directly. Bash excels at gluing together existing tools: a single script can call grep, awk, sed, curl, and dozens of other utilities in sequence.

The key patterns to master in Bash are conditional logic (if/then/else, case statements), loops (for iterating over files or lists, while for conditions), functions for code reuse, and error handling with exit codes. Input validation is critical because a script running with elevated privileges and a bug in a path variable can delete wrong directories. Always quote variable references to handle filenames with spaces: use "$VARNAME" not $VARNAME.

PowerShell for Windows administration

PowerShell is Microsoft's task automation framework, combining a command-line shell with a scripting language. Unlike Bash, which pipes text between commands, PowerShell pipes .NET objects. This means you can take the output of Get-Process and pass it directly to Where-Object to filter by CPU usage, then to Stop-Process to terminate matching processes, all in one pipeline without parsing text. PowerShell runs on Windows, Linux, and macOS (PowerShell Core / PowerShell 7+).

PowerShell's depth comes from its integration with Windows: it can manage Active Directory, Exchange, SharePoint, Azure, VMware, and hundreds of other products through purpose-built modules. Get-Command lists available cmdlets; Get-Help Name shows documentation; Get-Member shows what properties and methods an object has. These three commands are the foundation of learning PowerShell: when you do not know how to do something, Get-Command and Get-Help usually tell you.

Python for cross-platform automation

Python is a general-purpose language that runs on Windows, Linux, and macOS without modification. For IT automation, its strengths are working with APIs (the requests library makes HTTP calls straightforward), parsing JSON and CSV data, managing files and directories through the pathlib and os modules, and handling complex logic that would be awkward in Bash. Python's standard library covers most common automation needs, and the package ecosystem (pip) provides libraries for almost everything else.

Key Python patterns for IT automation: reading and writing files, making HTTP requests to REST APIs, parsing JSON responses, reading configuration files with configparser, and running subprocesses with subprocess.run(). Error handling with try/except prevents a single failed operation from stopping an entire automation workflow. For scheduled automation, Python scripts work well with cron on Linux or Windows Task Scheduler.

Automation best practices

Good automation code is readable, testable, and handles failures gracefully. Hardcode nothing that might change: put paths, URLs, credentials, and configuration in environment variables or separate configuration files, not in the script itself. Never put passwords or API keys in source code or commit them to version control. Log what the script does, especially what it modifies, so you can diagnose problems after the fact.

Test automation scripts in a non-production environment before running them against live systems. Use version control (Git) for all scripts, even short ones, so you can track changes and roll back if a modification introduces a problem. Idempotency is valuable in automation: a script is idempotent if running it multiple times produces the same result as running it once, with no harmful side effects on repeat execution. Configuration management tools like Ansible, Puppet, and Chef build idempotency in by design.

Key concepts

  • + Bash: Native Linux/macOS shell; excellent for gluing system tools together in sequential workflows.
  • + PowerShell: Windows-native, pipes .NET objects; integrates deeply with Microsoft products and Azure.
  • + Python: Cross-platform; best for API calls, data processing, and logic too complex for shell scripts.
  • + Error handling: Always check exit codes in Bash (set -e) and use try/except in Python; silent failures cause more damage than loud ones.
  • + No hardcoded secrets: Credentials, API keys, and passwords belong in environment variables or a secrets manager, never in the script.
  • + Idempotency: Scripts that can safely run more than once with the same outcome are far safer in automation pipelines.

Frequently asked questions

Should I learn Python, PowerShell, or Bash first?
Start with the one your current work environment uses most. If you work primarily on Linux servers, Bash first. If you manage Windows or Azure, PowerShell first. If you want a language that works everywhere and transfers to development and data engineering roles, Python first. After you learn one well, the others come faster because the concepts (loops, conditionals, functions, file I/O) are the same.
How do I run a script automatically on a schedule?
On Linux, use cron. Edit the crontab with crontab -e and add a line specifying the schedule and command. The format is: minute hour day-of-month month day-of-week /path/to/script.sh. On Windows, use Task Scheduler (GUI) or the Register-ScheduledTask PowerShell cmdlet to define the trigger and the command to run.
What is the difference between a script and a compiled program?
A script is interpreted at runtime by an interpreter (bash, python, powershell) rather than compiled ahead of time to machine code. This makes scripts easier to write, read, and modify, at the cost of somewhat slower execution. For IT automation, the difference in speed is rarely significant: scripts spend most of their time waiting on I/O (disk, network) rather than executing instructions. Compiled languages become important when raw compute performance is the bottleneck.
What is Ansible and how does it relate to scripting?
Ansible is a configuration management and automation tool that uses YAML-based playbooks to describe desired system states. Unlike shell scripts that run commands imperatively, Ansible modules are designed to be idempotent: an Ansible task that installs a package does nothing if the package is already installed. Ansible connects to remote hosts over SSH (or WinRM for Windows) without requiring an agent, making it easy to manage many machines from a single control node.

Related topics

AFFILIATE_SLOT_BOOKS Recommended books on scripting and automation

Reserved for affiliate links to recommended technical books and study guides. Not yet wired.

AFFILIATE_SLOT_COURSES Online courses for scripting and automation

Reserved for affiliate links to online learning platforms. Not yet wired.

LEAD_SLOT_NEWSLETTER Get scripting and automation reference updates

Self-hosted newsletter signup. Not yet wired to an email provider.

Technical Resources publishes vendor-neutral educational information about information technology. It is general reference material, not professional, safety, legal, or warranty advice, and it is not affiliated with, endorsed by, or sponsored by any vendor, certification body, or manufacturer named on the site. Product names, standards, and certification programs are referenced for identification and education only and belong to their respective owners. Technology, standards, exam objectives, and safety guidance change over time, so always verify any decision-critical detail against the current official documentation for your specific hardware, software, standard, or exam before you rely on it. Procedures involving electricity, batteries, or live equipment can be hazardous; follow the manufacturer's instructions and applicable safety codes, and consult a qualified professional when in doubt.