Writing Reusable Code

  • Bryan Cafferky


This chapter will cover how to use PowerShell to write the most robust reusable code possible. To do this, we will discuss how to use CmdletBinding in our functions. There are two types of CmdletBinding attributes: general attributes and parameter-specific attributes. The general attributes control how the overall function behaves. The parameter-specific attributes give us control over each parameter, including value validation. When a parameter is not provided, a default value is given. PowerShell defaults may not be what you expect, so we’ll cover what they are and how to handle them. We’ll also review how to define default values for parameters. Object-orientated languages support function overloading, which allows the same function name to be used with different types of parameters. We’ll discuss how to simulate this using parameter sets, and we’ll cover how to combine the pipeline with passed parameters to maximize the power and flexibility of your functions. Finally, in this age of globalization, we often need to support multiple languages, so we’ll discuss how multiple language support can be implemented easily with special PowerShell features.


Hash Table Data Section State Code Multiple Language Switch Parameter 
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.

Copyright information

© Bryan Cafferky 2015

Authors and Affiliations

  • Bryan Cafferky
    • 1
  1. 1.MAUS

Personalised recommendations