Skip to content

Scripting & Automation

From your first script to scheduled jobs: PowerShell, batch, zsh/bash, AppleScript, Shortcuts, cron and launchd.

Oddbyte walkthroughs

about_Execution_Policies — by Microsoft Learn

beginnerpage

Explains what PowerShell execution policies are, the available levels (Restricted, RemoteSigned, AllSigned, Bypass and others) and the scopes they apply to. It also stresses the policy is a safety net rather than a security boundary, and shows how to check and change it per user, machine or session. Loosening it (for example Unrestricted or Bypass) reduces protection, so prefer the narrowest scope.

Invok-Command on localhost not working — by Limitless Technology on Microsoft Q&A

intermediateforum

A user got Access denied running Invoke-Command against localhost even though WinRM was started. The answer explains Invoke-Command needs PowerShell remoting enabled even for the local machine, and suggests setting WinRM to Automatic, starting it and enabling remoting. The answer writes the cmdlet as EnablePSRemoting; the real name is Enable-PSRemoting. Enabling remoting opens a WinRM listener, so only do it where you need it.

PowerShell Remoting FAQ — by Microsoft Learn

intermediatepage

Question-and-answer guide to running PowerShell commands on other computers: how remoting works over WS-Management, Invoke-Command versus a persistent PSSession, throttling, serialized output and profiles not loading in remote sessions. It also covers workgroup and cross-domain caveats such as TrustedHosts, and remoting over SSH.

Enable-PSRemoting — by Microsoft Learn

advancedpage

Reference for the cmdlet that configures a Windows machine to receive remote PowerShell commands: it starts WinRM, creates listeners and firewall exceptions, and enables session configurations. It notes that public-network profiles are blocked on client Windows unless -SkipNetworkProfileCheck is used, and that PowerShell 7 and Windows PowerShell endpoints are configured separately. Remoting opens a listener, so only enable it where needed.

Batch & CMD

View section →

Errorlevel and exit codes - Windows CMD — by SS64.com

intermediatepage

Explains how batch files check whether a command succeeded using %ERRORLEVEL% and IF ERRORLEVEL, including the gotcha that IF ERRORLEVEL n means n or greater. It covers why %ERRORLEVEL% does not update inside bracketed blocks, differences between .BAT and .CMD, and EXIT /B for returning codes.

robocopy — by Microsoft Learn

intermediatepage

The full command reference for robocopy, with every copy, retry, logging and job option plus a table of exit codes. Useful for backup scripts: it shows /MIR, /MT, /R and /W, /LOG, and how exit code 8 and above signals a real failure while lower values only describe what was copied. /MIR and /PURGE delete destination files missing from the source, so test with /L first.

macOS Shell (zsh/bash)

View section →

tmutil - macOS Time Machine utility — by SS64.com

advancedpage

Reference for the tmutil command-line tool for Time Machine. It documents verbs such as restore (copies an item out of a snapshot, cp-style, may need root), listbackups and latestbackup (need root and Full Disk Access), plus addexclusion, startbackup and compare.

AppleScript & Shortcuts

View section →

AppleScript, Do Shell. How do I write the 'test' command to the 'script editor'? — by Quinn "The Eskimo!" (Apple DTS) on Apple Developer Forums

intermediateforum

Apple's DTS engineer explains that do shell script throws an AppleScript error whenever the command exits non-zero (so test -d on a missing folder errors), shows try blocks to handle it, multi-line scripts, and quoted form of POSIX path for safe paths. He also suggests avoiding the shell where System Events can do the job.

Scheduling (Task Scheduler, launchd, cron)

View section →

Run bash (.sh) via launchd using a plist in /Library/LaunchAgents — by VikingOSX and etresoft on Apple Community

intermediateforum

VikingOSX shows a working LaunchAgent for a shell script: plist in ~/Library/LaunchAgents with 644 permissions, Label, RunAtLoad, StartInterval and full paths only. The original poster got theirs working after moving the script out of ~/Documents, and etresoft points to privacy permissions instead, noting shell scripts silently fail without Full Disk Access.

Run PowerShell as Scheduled Task — by Microsoft Q&A community (accepted answer, anonymous) with Rich Matheisen

intermediateforum

A script that worked by hand failed under Task Scheduler. The accepted answer says not to tick Do not store password when using Run whether user is logged on or not, since the task then cannot reach network resources, and to isolate the problem with a trivial test script and -noexit. Rich Matheisen recommends Start-Transcript for diagnosis and says two-factor authentication won't work in a scheduled task; the poster suspected a smart-card PIN prompt. No confirmed final fix is recorded.

Video Walkthroughs

View section →