liveBook · Manning

Param block

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

A param block is a PowerShell language construct that declares the parameters and default values of a script or function.

A param block turns changing script values into command-line inputs. It can assign defaults, so a script works without every value being supplied. section 19.4

Use a param block to:

  • Declare one or more parameters, separated by commas. section 19.4
  • Accept values positionally or by name, including abbreviated names. section 19.4
  • Add data types, mandatory behavior, and parameter attributes. section 20.3

Disk inventory parameters

param ( $computername = 'localhost', $drivetype = 3 ) lets the script query the local computer by default. Run .\Get-DiskInventory.ps1 SRV1 3, or omit either value to use its default. section 19.4

Listing 19.3 Get-DiskInventory.ps1, with an input parameter
param (
  $computername = 'localhost'      #1
)
Get-CimInstance -class Win32_LogicalDisk -computername $computername `
 -filter "drivetype=3" |
 Sort-Object -property DeviceID |
 Format-Table -property DeviceID,
     @{label='FreeSpace(MB)';expression={$_.FreeSpace / 1MB -as [int]}},
     @{label='Size(GB';expression={$_.Size / 1GB -as [int]}},
     @{label='%Free';expression={$_.FreeSpace / $_.Size * 100 -as [int]}}

Inspect a complete parameterized disk-inventory script that passes the computer name into a CIM query. section 19.4

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Explains declarations, defaults, invocation forms, types, mandatory attributes, and block punctuation through working scripts.
Examples
Uses Get-DiskInventory with computer-name and drive-type parameters, including mandatory prompting and typed values.
What you can do after
Apply it via PowerShell scripts to replace hardcoded values with named, positional, defaulted, or mandatory inputs.
How it connects
Connects parameters with scripts, default values, positional arguments, parameter attributes, and pipeline input.

Also explained in

Compare how 2 books explain param block →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.