Featured resource
pluralsight tech forecast
2025 Tech Forecast

Which technologies will dominate in 2025? And what skills do you need to keep up?

Check it out
Hamburger Icon
  • Labs icon Lab
  • Core Tech
Google Cloud Platform icon
Labs

Create an Advanced PowerShell Function for System Optimization

In this lab, you'll practice building a professional-grade PowerShell function. You'll have a reusable and well-documented PowerShell function when you're finished.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 1h 45m
Published
Clock icon Aug 12, 2024

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Getting Started in the Lab Environment

    Here are the initial instructions and explanation of the lab environment. Read this while your environment is busy creating itself from nothing. Yes, this violates physics; we know. How fun!

  2. Challenge

    Create a Function Skeleton with CmdletBinding

    You will begin by constructing the foundational elements of a PowerShell function aimed at a fictional scenario around system optimization. Named Optimize-LocalSystem, this function will begin as a basic structure. You'll particularly focus on the CmdletBinding attribute, a key element that enables advanced features like WhatIf and Confirm. By the end of this short challenge, you'll have a skeletal framework for your Optimize-LocalSystem function, and an understanding of why CmdletBinding is crucial for advanced functionalities in PowerShell functions.

  3. Challenge

    Add the First Parameter

    You'll continue to build upon the Optimize-LocalSystem function. Your objective is to make the function more versatile by adding the ability to perform different operations: either checking for system updates or cleaning temporary files.

  4. Challenge

    Add WhatIf and Confirm Support

    In this challenge, you'll focus on adding support for WhatIf and Confirm, specifically for the CleanTempFiles operation. This is important because deleting files is a sensitive task that could benefit from additional user confirmation, or even a "dry run" to simulate the outcome.

  5. Challenge

    Implement Parameter Validation

    Dive into parameter validation in PowerShell functions for reliability. You will begin with the [ValidateSet] attribute to limit inputs, make crucial parameters mandatory, and explore dynamic parameters appearing under specific conditions. By the end, you'll enhance function resilience and effectiveness.

  6. Challenge

    Add Error Handling

    When your script fails silently due to non-terminating errors, it threatens reliability. This challenge teaches you to detect and address these errors, enhancing script robustness and trust.

  7. Challenge

    Implement Parameter Sets

    Enhance your PowerShell function to run operations when given input manually, and when given a CSV file as input, with one command. This challenge makes your function more versatile and efficient.

  8. Challenge

    Add Pipeline Support

    The challenge refines Optimize-LocalSystem to support pipeline input. It emphasizes the begin, process, and end blocks for efficient pipeline handling. Furthermore, the Operation parameter accepts pipeline input by name, optimizing batch operations in PowerShell.

  9. Challenge

    Write Help Content

    Your Optimize-LocalSystem function is pretty robust, but could it be "user-friendlier"? That's where comment-based help comes in. By the end of this challenge, users will be able to type Get-Help Optimize-LocalSystem and get all the details they need to use your function like a pro.

  10. Challenge

    Manage Output

    In the IT realm, predictability is a virtue. Imagine scripting functions that don't just end at producing results, but those results are intuitive, structured, and empower further actions. You will use the OutputType attribute, which will ensure your function only returns what’s necessary.

Adam Bertram is an independent consultant, technical writer, trainer, and presenter. Adam specializes in consulting and evangelizing all things IT automation, mainly focused around Windows PowerShell.

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.