Appendix B. Modules and advanced functions

 

Functions have been a useful part of PowerShell v1, in that they allow us to reuse code and cut down the size of our scripts by avoiding repetition. We can also save the functions into memory so that they’re available from within our PowerShell session. It’s possible to organize functions into libraries and perform a bulk load. These functions can be used from the command line or within scripts. The structure of functions was discussed in section 2.4.5.

PowerShell v2 provides a new way of organizing functions into modules. A module can be a collection of functions or even a compiled DLL. Windows 7 and Windows Server 2008 supply their PowerShell functionality as modules, as we saw in chapter 15. We’ll discover how to write our own modules and how we can use this functionality.

If we want to use our PowerShell v1 functions on the pipeline, we have to create them as filters because a function only runs once. A filter will run once for every object on the pipeline. Advanced functions in PowerShell v2 enable us to write our functions and use them as if they were cmdlets—on the command line, in scripts, and on the pipeline.

B.1. Modules

B.2. Advanced functions

B.3. Recommendations