liveBook · Manning

CmdletBinding attribute

As explained in Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

The CmdletBinding attribute is a PowerShell function attribute that enables cmdlet-like features, including common parameters and advanced parameter behavior.

Add [CmdletBinding()] to a PowerShell function to turn it into an advanced function. PowerShell then supplies common parameters such as -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutVariable, and -PipelineVariable. section 11.1

Use the attribute as the foundation for practical automation features:

  • Accept pipeline input by combining CmdletBinding with [Parameter(ValueFromPipeline=$True)] and processing input through BEGIN, PROCESS, and END blocks. section 11.1.1
  • Support -WhatIf and -Confirm by setting SupportsShouldProcess=$True, choosing ConfirmImpact, and guarding risky operations with $PSCmdlet.ShouldProcess(). section 11.1.5
  • Extend parameter design with mandatory parameters, validation, aliases, parameter sets, and—when appropriate—dynamic parameters. section 11.3 section 11.1.4

Upgrade a function

function Get-MachineInfo { [CmdletBinding()] Param( [Parameter(ValueFromPipeline=$True)] [string[]]$ComputerName ) BEGIN { } PROCESS { foreach ($computer in $ComputerName) { # Query each computer here } } END { Write-Output "All Done" } } Get-MachineInfo -ComputerName ONE,TWO,THREE "ONE","TWO","THREE" | Get-MachineInfo The same function can accept direct arguments or pipeline input, while its help syntax exposes [<CommonParameters>]. section 11.1.1

Function Set-Something {
  [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
  Param(
  )
} #function

Use SupportsShouldProcess and ConfirmImpact when a function changes system state; pair them with ShouldProcess around operations that need WhatIf or confirmation. section 11.1.5

How Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools covers it

How deep it goes
Explains implementation through function comparisons and examples, then defers deeper parameter sets and dynamic parameters.
Examples
Uses Get-MachineInfo to demonstrate help, common parameters, direct and pipeline invocation, and safe system-changing operations.
What it assumes
Assumes basic PowerShell functions and parameter blocks while developing advanced behavior incrementally from a simple function.
What you can do after
Build it from scratch as an advanced function with common parameters, pipeline processing, and optional WhatIf or Confirm support.
How it connects
Connects advanced functions with common parameters, pipeline input, parameter attributes, ShouldProcess, WhatIf, Confirm, and parameter sets.

Also explained in

Compare how 2 books explain CmdletBinding attribute →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools. The section links point to the parts of the book it draws on.