DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk7 min

How to Write Automation Scripts in Python, Bash, and PowerShell

Learn how to choose Python, Bash, or PowerShell for automation, write a safe first script, handle external commands and failures, and prepare it for scheduling.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the scripting language that fits the machine and work you need to automate: Python is a practical choice for structured data and more involved program logic, Bash for composing commands on Unix-like systems, and PowerShell for Windows and Microsoft administration workflows. These are task-fit heuristics, not performance rankings. In any of them, a reliable script makes its inputs, assumptions, and failure behavior explicit before you schedule it.

Choose a language that fits the task

Before writing code, list the operating system, files or services the task touches, commands or libraries it needs, expected inputs, and what should happen if a step fails. Then choose the environment that already fits that work.

Language Good fit Keep in mind
Python Structured data, filesystem work, and scripts with richer program logic or library needs. Use the standard library for ordinary file and path operations where it fits; subprocess calls need careful argument handling.
Bash Composing commands in Unix-like environments. Quoting affects whether characters are treated literally or as shell syntax; check command statuses and pipeline behavior.
PowerShell Windows tasks and Microsoft administration; it is both a command-line shell and a scripting language. It can run native commands and PowerShell commands such as cmdlets, but parsing and error behavior are not identical to Bash.

Also check which language and runtime are available wherever the script will run. A local terminal, a server, and a managed automation service may use different accounts, environments, and supported versions.

Start with a small, safe task

The equivalent examples below create a directory named automation-output in the current working directory if it does not already exist. They do not delete or overwrite files. Run them in the intended environment and confirm the working directory before relying on that location.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Python

Save as make_directory.py and run with Python 3:

from pathlib import Path

output_dir = Path.cwd() / "automation-output"
output_dir.mkdir(exist_ok=True)
print(f"Ready: {output_dir}")

Python has filesystem and path tools in its standard library, so an external shell command is not necessary for this operation. The Python 3.10 subprocess reference also points to utilities such as glob, os.walk, shutil, and path helpers for tasks often delegated to shell commands: Python subprocess documentation.

Bash

Save as make_directory.sh, then run it with Bash. The example uses mkdir -p so an existing directory is not an error:

#!/usr/bin/env bash
set -o pipefail

output_dir="${1:-automation-output}"
if ! mkdir -p -- "$output_dir"; then
  printf 'Could not create directory: %sn' "$output_dir" >&2
  exit 1
fi
printf 'Ready: %sn' "$output_dir"

For example, bash make_directory.sh "weekly output" passes one argument containing a space. Quoting "$output_dir" preserves it as one argument. Bash quoting removes special meanings from characters or words; unquoted expansions can therefore change how a command is interpreted. The Bash manual also defines how scripts receive their name as $0 and arguments as positional parameters: Bash Reference Manual, Edition 5.3.

PowerShell

Save as Make-Directory.ps1 and run it in PowerShell:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
param(
    [string]$Path = "automation-output"
)

try {
    $directory = New-Item -ItemType Directory -Path $Path -Force -ErrorAction Stop
    Write-Output "Ready: $($directory.FullName)"
}
catch {
    Write-Error "Could not create directory '$Path': $_"
    exit 1
}

Pass a path with spaces as one quoted argument, for example ./Make-Directory.ps1 -Path "weekly output". PowerShell commands such as New-Item are cmdlets; native operating-system commands have their own parsing and status behavior. Microsoft explains the distinctions in its PowerShell 7.6 guide to running commands.

Make scripts safe to rerun

An automation script should behave predictably when run twice, with missing or malformed input, and when a dependency fails. Before putting one on a schedule, add the controls its task needs:

  • Inputs: accept arguments or read configuration deliberately. State defaults, validate required values, and reject invalid paths or unsupported choices before making changes.
  • Scope: make the working directory and target paths clear. Prefer explicit paths when the scheduler’s starting directory may differ from your interactive shell.
  • Repeatability: design steps to be safe on a second run. Where a task changes data, distinguish create, update, and delete operations and consider how to recover from a partial run.
  • Visibility: log useful milestones and errors, but avoid printing passwords, access tokens, or sensitive data.
  • Failure contract: decide which failures stop the script, which are expected, and what exit status should tell the caller. Do not report success after a required step fails.
  • Testing: first run with harmless sample inputs or a test destination. Check both the resulting changes and the status returned to the shell or scheduler.

Run external commands carefully

Python: prefer an argument list

When a task genuinely needs an external program, Python’s subprocess.run is a suitable starting point. Passing arguments as a sequence is generally preferred: Python can handle the quoting and escaping needed to pass them to the program.

import subprocess

subprocess.run(["python", "--version"], check=True, text=True)

check=True raises an exception if the command exits unsuccessfully, rather than silently treating a nonzero exit as success. Handle that exception at the appropriate level if the script needs a custom recovery or message. Avoid building a single command string from user input.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

shell=True is an explicit choice, not a default requirement. It asks for shell parsing and carries security considerations, especially if any part of the command comes from untrusted input. Use it only when shell features are genuinely needed and inputs are handled carefully. See the Python 3.14.8 subprocess documentation.

Bash: quote expansions and inspect statuses

Quote variable expansions and filenames unless you specifically need the shell to interpret their contents. A command’s exit status can guide the next step; use conditional forms such as if ! command; then ... fi when you need an explicit response to failure.

set -e is not a guarantee that every nonzero command will stop a script. Bash documents contexts where it does not exit, so understand those exceptions rather than treating it as complete error handling. set -o pipefail changes a pipeline’s reported status so a failing component can affect the result; decide whether that is appropriate for the pipeline you use. The sample directory script uses an explicit check for the operation that matters.

PowerShell: account for streams and native commands

PowerShell has six output streams; Bash and cmd.exe use stdout and stderr. Its parsing rules and handling of native process errors differ from Bash, and native-command behavior is version-specific. Do not assume a command or error-handling pattern copied from Bash behaves the same in PowerShell.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Start-Process when you need process-control features such as credentials, redirected streams, or a different working directory. Microsoft recommends it when that level of control is required; for straightforward commands, use the ordinary command invocation appropriate to the task. Consult the PowerShell guide for details that match your installed version.

Schedule separately from script logic

A scheduler launches a process; it does not automatically recreate your interactive session. The scheduled run may use a different account, permissions, working directory, environment variables, installed modules, and runtime. Test under the same identity and conditions the scheduler will use, and make required paths and configuration explicit.

Managed services impose their own runtime support matrix. For example, Microsoft’s Azure Automation runbook documentation lists PowerShell 7.6, 7.4, and 5.1 and Python 3.10 in that service context, and says Azure Automation follows the support lifecycles of PowerShell and Python. Those versions describe Azure Automation, not every computer or scheduler; check the service’s current documentation before deployment: Azure Automation runbook types.

Troubleshoot common failures

  • A file or directory is not found: the scheduled working directory may differ from the one used in your terminal. Print or log the current directory during diagnosis, then use an explicit path or set the scheduler’s working directory.
  • A command works interactively but not in automation: the scheduler may run as another account or lack the executable, module, environment variable, or permission. Verify those requirements as the scheduled identity.
  • A path containing spaces is split: quote the Bash variable expansion or pass a single quoted argument in PowerShell. In Python, pass a list of arguments to subprocess.run rather than assembling a command string.
  • A script continues after an important command fails: check that command’s status explicitly. In Bash, do not rely on set -e alone; in Python, consider check=True for subprocess calls; in PowerShell, use error handling appropriate to whether the failing operation is a cmdlet or a native command.
  • A native command’s error looks different across PowerShell versions: verify the installed version and its native-command behavior, then choose handling that matches it rather than assuming Bash semantics.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If the task is taking a website screenshot rather than automating a local workflow, ScreenshotNeo offers a one-request API:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation. It accepts cookie and consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, and responses identify page verdict and billing status in headers. Its MCP server lets AI agents using Claude, Cursor, or other MCP clients call screenshot tools. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000.

Sign up for ScreenshotNeo’s free plan to try it without a card. ScreenshotNeo is made by Yorker Media: ScreenshotNeo.

Frequently Asked Questions

Should I learn all three scripting languages?

Not necessarily. Start with the environment your task and deployment target already use; learn another when its native tools or runtime support materially simplify the work.

Can I run Bash scripts in PowerShell unchanged?

No. The shells differ in parsing, command types, streams, and error behavior. Run a script with the shell it was written for or adapt it deliberately.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Does Azure Automation’s runtime list apply to my computer?

No. It applies to Azure Automation runbooks. Check the runtime support information for your own host or automation service.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.