liveBook · Manning

Chapter 20: Improving your parameterized script

What you will learn in chapter 20 of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

The introduction explains that parameters let people run a script without changing its contents.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains that parameters let people run a script without changing its contents.

Key terms: parameterized script, parameters

20.1 Starting point

This section changes a parameterized script to output objects instead of a formatted table so its results can be used in different ways.

Key terms: Select-Object, Format-Table, objects, preformatted output

20.2 Getting PowerShell to do the hard work

Adding [CmdletBinding()] in the required position turns the existing script into an advanced script without changing how it normally runs.

Key terms: [CmdletBinding()], advanced script, comment-based help

20.3 Making parameters mandatory

The section shows how to require a script parameter and explains how parameter declarations affect the prompt users see.

Key terms: -ComputerName, [Parameter(Mandatory=$True)], mandatory parameter, Param() block, decorators, Mandatory attribute, [string], [int], parameter name, HelpMessage

20.4 Adding parameter aliases

This section shows how to give a script parameter an alternative name while keeping its conventional PowerShell name.

Key terms: -ComputerName, -host, parameter alias, decorator, parameter name, alias

20.5 Validating parameter input

The section explains why the script should restrict its drive-type parameter and identifies ValidateSet() as the validation technique to use.

Key terms: -drivetype, Win32_LogicalDisk, ValidateSet()

20.6 Adding the warm and fuzzies with verbose output

This section shows how a parameterized script can provide optional verbose progress messages and include variable values in those messages.

Key terms: Write-Verbose, Write-Host, [Cmdlet-Binding()], -Verbose, $VerbosePreference, $computername, double quotation mark trick

20.7 Lab

20.8 Lab answer

Where this is used later

Concepts explained in this chapter

← Chapter 19: You call this scripting? Chapter 21: Using regular expressions to parse text files →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.