Best practices for functions, switches, and loops

When you are scripting, you will find that you frequently need to utilize functions, loops, switches, and methods. Each of these code structures enable you to produce code faster so that you don't have to repeat code within your script. As you work with each of these structures, there are several best practices that you can follow.

Best practices for functions

There are a few recommended steps that can be followed to obtain optimum performance from functions. They are listed as follows:

  • If you need to execute a sequence of code more than once, you should create a function. This will allow you to quickly repeat the same action without significantly increasing the size of the script.
  • If you need to pass ...

Get Mastering Windows PowerShell Scripting 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.