PowerShell scripting: functions, modules and error handling
You write reusable PowerShell scripts with advanced functions, parameter validation, modules and robust error handling.
Level 201 · 18 lessons · 15 practice labs
Lessons
Advanced functions
- Advanced functions with CmdletBinding
- Parameter validation: Mandatory, ValidateSet, ValidateScript
- Taking pipeline input: begin, process, end
- Supporting -WhatIf and -Confirm
- Writing comment-based help
- Short task: a function that takes users from the pipeline and produces a report
Errors and logging
- Terminating and non-terminating errors
- try, catch, finally and the error record
- Streams: Verbose, Warning, Information and logging
- Set-StrictMode and safe defaults
- Short task: a copy script that resumes where it stopped
Modules
- Writing a script module: .psm1 and exports
- The module manifest: .psd1 and versions
- Installing and trusting modules from the PowerShell Gallery
- Short task: gather the team's functions into one module
PowerShell on Linux
- pwsh on Linux: paths, case sensitivity and native commands
- Turning native command output into objects
- Capstone: a PowerShell module that collects an inventory of Linux servers
Practice labs
- Turn a function into an advanced function: Add CmdletBinding and a mandatory parameter to a simple function and show it gives extra detail with -Verbose.
- Write a report function that takes users from the pipeline: Write a function that processes user objects from the pipeline in its process block and summarises in its end block.
- Add -WhatIf and -Confirm to a delete function: Add ShouldProcess support to a function that deletes old backups and prove nothing is removed with -WhatIf.
- Reject bad input with parameter validation: Add ValidateSet and ValidateRange to the environment and port parameters and confirm invalid values are refused.
- Send output to the right streams: Rework a script that prints everything with Write-Host to use the Verbose, Warning and Information streams and log warnings to a file.
- Write a module that collects a Linux server inventory: Write a module whose functions return kernel version, package count and listening ports as objects.
- Make an uncaught error catchable: Make a non-terminating error that slips past try/catch catchable with -ErrorAction Stop and print a clear message.
- Write a copy script that resumes where it stopped: Write a script that skips finished files and resumes after an interruption, updating a state file in finally.
- Expose hidden bugs with StrictMode: Find and fix the misspelt variable and missing property bugs that appear once Set-StrictMode is on.
- Gather the team's functions into one module: Move functions scattered across script files into a .psm1 module and export only the public ones.
- Add a manifest and version to a module: Create a .psd1 defining version, author and exported functions for the module and pass Test-ModuleManifest.
- Find why a module will not load: Find and fix the wrong path, missing export and version conflict problems in a module that errors on import.
- Fix paths and case errors that break on Linux: Make a script that fails on Linux over backslash paths and case sensitivity portable using Join-Path.
- Turn native command output into objects: Turn df output into objects with filesystem, size and use properties and filter those above eighty per cent.
- Catch a native command's failure in a script: Fix a script that carries on after a failing native command so it checks the exit code and throws an error.
About this course
You write reusable PowerShell scripts with advanced functions, parameter validation, modules and robust error handling.
What you will learn
- Advanced functions
- Errors and logging
- Modules
- PowerShell on Linux
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.