Chapter 7. PowerShell functions

 

This chapter covers:

  • 7.1 Fundamentals of PowerShell functions
  • 7.2 Declaring formal parameters for a function
  • 7.3 Returning values from functions
  • 7.4 Using simple functions in a pipeline
  • 7.5 Managing function definitions in a session
  • 7.6 Variable scoping in functions
  • 7.7 Summary

Porcupine quills. We’ve always done it with porcupine quills.

Dilbert

In this chapter, we’ll begin looking at how to combine the features from the previous chapters into reusable commands. As you’ll recall from chapter 2, there are four types of PowerShell commands: functions, cmdlets, scripts, and external commands. Functions and scripts are the two command types that can be written in the PowerShell language. We’ll start with functions as they’re the simpler of the two and are also easy to enter interactively in a session. In the next chapter we’ll expand our discussion to include scripts as well as introduce advanced programming features available to both functions and scripts.

7.1. Fundamentals of PowerShell Functions

7.2. Declaring Formal Parameters for a Function

7.3. Returning Values from Functions

7.4. Using Simple Functions in a Pipeline

7.5. Managing Function Definitions in a Session

7.6. Variable Scoping in Functions

7.7. Summary