Chapter 6. PowerShell functions

 

This chapter covers

  • Fundamentals of PowerShell functions
  • Function parameters and return values
  • Functions in the pipeline
  • Variable scoping

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

Dilbert

In this chapter we’ll begin looking at how to create reusable commands by combining the features from the previous chapters. Functions and scripts are the two command types that can be written in the PowerShell language. (Cmdlets and external commands are written in a language such as C# that can be compiled.) We’ll start with functions because they’re simpler. In the next chapter, we’ll cover scripts as well as introduce advanced programming features available to both functions and scripts.

Note

The functions you’ll see in this chapter are simple functions. Chapter 7 discusses advanced functions, including how to turn a simple function into an advanced function. These two chapters are the foundation for modules that we discuss in chapters 8 and 9.

6.1. Fundamentals of PowerShell functions

6.2. Declaring formal parameters for a function

6.3. Returning values from functions

6.4. Using simple functions in a pipeline

6.5. Managing function definitions in a session

6.6. Variable scoping in functions

6.7. Summary

sitemap