Automation
Turn your one-off commands into real, reusable .ps1 scripts that handle errors gracefully and run themselves on a schedule — exactly how working sysadmins automate the boring stuff.
Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.
Part of the free Powershell course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
1. From Commands to a Script File
A script is simply the commands you've been typing, saved into a file ending in .ps1 . You run it by name. Adding a param() block at the very top turns it from a one-trick file into a reusable tool: the caller passes values in, so the same script works for many situations. Read this, save it as save-me.ps1 , and run it.
2. The Execution Policy
The first time you try to run a script, Windows often refuses with "running scripts is disabled on this system." That's the execution policy — a safety gate so a downloaded file can't run code behind your back. The fix is to set it to RemoteSigned : your own local scripts run freely, but anything downloaded from the internet must be digitally signed first.
3. Handling Errors with try/catch/finally
A scheduled script runs with no human watching, so it must cope with failure. Wrap risky work in try {' … '} ; if it throws, the matching catch {' … '} runs your recovery code; finally {' … '} always runs (great for cleanup). The catch is the gotcha: most cmdlet errors don't throw by default — they just print red text and the script keeps going. Add -ErrorAction Stop (or set $ErrorActionPreference = 'Stop' ) to make them throw so catch can actually fire.
Your turn. Fill in the two blanks marked ___ so this script catches a missing file instead of crashing, then run it.
4. Modules — Reusing Other People's Code
A module is a packaged set of cmdlets. Install-Module downloads one from the PowerShell Gallery (once per machine); Import-Module loads it so you can use its commands in the current session. This is how you get powerful tools — like PSScriptAnalyzer , which lints your scripts for mistakes — without writing them yourself.
5. Scheduling a Script to Run Itself
The payoff: tell Windows to run your script on a timer. A scheduled task is built from three parts — an Action (what to run), a Trigger (when), and Settings (how) — then registered with Register-ScheduledTask . On Linux or macOS the same job is a one-line cron entry ( 0 2 * * * pwsh -File … ); the script is identical, only the scheduler changes.
Here's a complete, schedulable script that uses everything from this lesson: param() inputs, $ErrorActionPreference = 'Stop' , a pipeline to find old files, try/catch , and a -WhatIf dry-run switch so you can preview before deleting. Read it line by line — you understand every part now.
Why -WhatIf ? Anything destructive ( Remove-Item , Copy-Item ) supports it. Passing -WhatIf:$WhatIf flows your switch straight through, so one script does both a safe preview and the real run.
No blanks this time — just a brief and an outline. Write backup.ps1 yourself, test it with a real folder, then schedule it. This is the exact kind of script that runs in production every night.
That's the whole PowerShell course. You can now:
Where next? Put it to work: automate a chore on your own machine (backups, log cleanup, a report). Then deepen your toolkit with the Git course to version-control your scripts, or the Python course for cross-platform scripting. Keep automating the boring stuff — that's the whole job.
Practice quiz
What file extension does a PowerShell script use?
- .ps1
- .psh
- .pow
- .sh
Answer: .ps1. PowerShell scripts are saved in files ending in .ps1.
Where must the param() block appear in a script?
- At the very end
- As the first code in the file
- Inside a function only
- Anywhere
Answer: As the first code in the file. param() must be the first executable code in a script file.
Which execution policy lets your local scripts run but gates downloaded ones?
- Restricted
- Bypass
- RemoteSigned
- Unrestricted
Answer: RemoteSigned. RemoteSigned runs your local scripts and requires downloaded ones to be signed.
Which block always runs, whether or not an error occurred?
- try
- catch
- trap
- finally
Answer: finally. The finally block always runs, making it ideal for cleanup.
How do you make a cmdlet error catchable by try/catch?
- Add -ErrorAction Stop
- Add -Catchable
- Use return
- Use Write-Error
Answer: Add -ErrorAction Stop. -ErrorAction Stop turns a non-terminating error into a catchable exception.
Which cmdlet downloads a module from the PowerShell Gallery?
- Import-Module
- Install-Module
- Get-Module
- Add-Module
Answer: Install-Module. Install-Module downloads and installs a module (once per machine).
Which cmdlet loads an installed module into the current session?
- Get-Module
- Install-Module
- Import-Module
- Use-Module
Answer: Import-Module. Import-Module loads a module's commands into the current session.
Which cmdlet registers a scheduled task on Windows?
- New-CronJob
- Add-Schedule
- Set-Timer
- Register-ScheduledTask
Answer: Register-ScheduledTask. Register-ScheduledTask creates a task from an action, trigger, and settings.
What is the cross-platform equivalent of a scheduled task on Linux/macOS?
- cron
- systemd-only
- Task Manager
- launchpad
Answer: cron. On Linux and macOS you use cron, e.g. a crontab entry, to schedule scripts.
Inside a catch block, how do you read the error message?
- $error.text
- $_.Exception.Message
- $catch.msg
- $LastError
Answer: $_.Exception.Message. $_ holds the thrown error; $_.Exception.Message is its text.