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



















