Appendix A. Implementing Reusability with Functions and Modules

We have covered quite a few PowerShell snippets throughout the book. Instead of re-typing these snippets over and over again however, we can start writing and organizing them in a way that makes them reusable. In this appendix, we will explore how to create functions and script modules in PowerShell.

Functions

One way to wrap your script into something reusable and flexible is by converting it into a function. A function, also called a subroutine in other programming languages, is defined as a named group of statements that perform a specific task.

A PowerShell function does exactly that. It wraps lines of code into a single named construct and does a specific task. You can create simple ...

Get PowerShell for SQL Server Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.