liveBook · Manning

Function

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

A function is a named, reusable block of PowerShell instructions that can be invoked as part of a script.

A function is a named, reusable block of PowerShell instructions that acts as a tool. Keep each function tightly scoped—ideally doing one thing—and self-contained: accept declared input parameters, avoid hidden external dependencies, and send intended output through the pipeline. section 10.1 section 6.1

Design functions around a clear contract:

  • Declare inputs as parameters; parameter names become variables inside the function, and types such as [string], [int], [datetime], [switch], and [string[]] communicate expected values. section 10.1.1
  • Return structured objects to the PowerShell pipeline rather than using output for messages. Pipeline objects can be selected, filtered, or converted into reports by later commands. section 10.1 section 17.1.1
  • Prefer generic names and paths over hardcoded context, and separate reusable tools from controller scripts that apply them to a larger workflow. section 17.1.2 section 6.1
  • Split complex sequences into several functions so each boundary can be written, tested, debugged, and maintained independently. section 17.1.3 section 17.2.2

Service-logon function

A practical function can expose the task through parameters and support one or many computers: Set-TMServiceLogon -ServiceName OurService -NewPassword "P@ssw0rd" -NewUser "COMPANY\User" -ComputerName SERVER1,SERVER2. Omitting -NewUser demonstrates a second valid usage shape. The exercise places the function in the MolTools script module and tests it against BITS through a WSMan/CIM connection. section 10.5.2

Composable folder tools

Get-FolderSize accepts pipeline input, measures files beneath a path, and emits a consistent object containing Path, Files, and Bytes. Get-UserHomeFolderInfo then calls it while checking folders against Active Directory, keeping generic measurement separate from task-specific orphan detection. section 17.1.2 section 17.1.3

Set-TMServiceLogon -ServiceName OurService
                   -NewPassword "P@ssw0rd"
                   -NewUser "COMPANY\User"
                   -ComputerName SERVER1,SERVER2
Set-TMServiceLogon -ServiceName OurService
                   -NewPassword "P@ssw0rd"
                   -ComputerName SERVER1,SERVER2

Compare two invocations of Set-TMServiceLogon to see how optional parameters change a function’s command contract. section 10.5.2

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

How deep it goes
Explains parameters, pipeline output, scope, composition, and modular decomposition, while deferring more advanced parameter construction.
Examples
Uses service-logon automation, folder-size reporting, orphan-folder detection, and password-expiration notification functions.
What it assumes
Assumes basic PowerShell command and pipeline familiarity while teaching simple parameter declarations and function structure.
What you can do after
Build it from scratch as a reusable PowerShell tool, place it in a script module, test it, and compose it into larger automation.
How it connects
Connects functions with parameters, pipelines, objects, script modules, cmdlets, and controller scripts.

Also explained in

Compare how 20 books explain function →

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.