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?
How do I run a script automatically on a schedule?
What is the difference between a script and a compiled program?
What is Ansible and how does it relate to scripting?
Related topics
Reserved for affiliate links to recommended technical books and study guides. Not yet wired.
Reserved for affiliate links to online learning platforms. Not yet wired.
Self-hosted newsletter signup. Not yet wired to an email provider.