PowerShell scripts, functions and modules
You write reusable PowerShell tools with advanced functions that take parameters, proper error handling and modules.
Level 201 · 16 lessons · 15 practice labs
Lessons
Writing scripts
- Script layout, execution policy and signed scripts
- Variables, scopes and strict mode
- Conditions, loops and switch
- String formatting and regular expressions
- Short task: write a script that reports disk usage
Advanced functions
- Parameter blocks and validation
- Taking pipeline input: begin, process, end
- Error handling: try/catch, ErrorAction
- Safe changes with WhatIf and Confirm
- Writing comment-based help
- Short task: write a function that lists stale accounts with WhatIf first
Modules and data
- Script modules and manifests
- Installing modules and an offline repository
- Working with CSV, JSON and XML
- Short task: package your functions as a module and share it
Wrap-up
- Capstone: write a server health report module
Practice labs
- A total that’s zero because of a typo: Expose the bug in a script that silently returns zero because of a misspelled variable by running it in strict mode, fix it and show the right total.
- A counter that never goes up: Find the scope bug that leaves a counter at zero outside even though a function increments it, fix it without resorting to a global variable and show the script prints the right count.
- Three quiet bugs: Fix a script with an array count that breaks on a single result, a $null comparison on the wrong side and a string-versus-number comparison, and show the given check script passes in every case.
- An aligned disk report: Turn the given disk list into a text report with aligned columns, GB values to two decimals and percent used, using the format operator.
- Objects from log lines: Turn the lines of Blue Harbour Cargo’s delivery log into objects using named capture groups, count malformed lines separately and export the result as CSV.
- Parse a config written in blocks: Parse a network device configuration made of multi-line blocks, turn each block into an object with its properties, handle comments and missing fields correctly and save it as JSON.
- Stop bad input at the door: Add mandatory, value-set and range validation to the function’s parameters and show three invalid calls are rejected before the body runs.
- A function that handles only the last item: Fix the function that handles only the last of a piped server list using a process block and binding by property name, and show every item is processed.
- A function with two ways to call it: Write a function that can be called by name or by file path, accepts pipeline input and has help with examples, and prove with the given check script that the two parameter sets never mix.
- A try/catch that catches nothing: Fix the script that errors on a missing file but never enters its catch block by making the error terminating, and show the error is written to the log.
- Show before you delete: Add WhatIf and Confirm support to the clean-up function that deletes old files, and show nothing is deleted with -WhatIf and only old files are deleted without it.
- A bulk run where half the items fail: Rewrite the script that stops at the first error while processing hundreds of records so it skips bad records and continues, logs error records with target and category, prints a summary and exits with the right code.
- JSON from a CSV inventory: Read Selvi Schools’ computer inventory from CSV, filter the machines out of warranty, sort by date and save the result as a JSON file.
- Functions that don’t show up: Fix the manifest of a module whose public functions don’t appear on import while an internal helper is exposed, and show the right exports with Get-Command.
- Two versions in an offline repository: Register a local folder as a module repository, publish two versions of your module, install a specific version and prove both sit side by side with the requested one loading.
About this course
You write reusable PowerShell tools with advanced functions that take parameters, proper error handling and modules.
What you will learn
- Writing scripts
- Advanced functions
- Modules and data
- Wrap-up
Prerequisites
Finishing the level 101 courses of the same path, or knowing those topics, is enough.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 201; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.