Book Image

Mastering Windows PowerShell Scripting

By : Brenton J.W. Blawat
Book Image

Mastering Windows PowerShell Scripting

By: Brenton J.W. Blawat

Overview of this book

Table of Contents (22 chapters)
Mastering Windows PowerShell Scripting
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Functions


When you need to query or execute code more than once, the general rule is that you should create a function to perform the action. Functions are blocks of reusable code, which you can execute multiple times by calling the function's name. You must place a function near the beginning or top of the script. This allows PowerShell to interpret the whole function before you use it later in the code. All other code, including invoking the functions, should follow the functions section. If you call a function that has not yet been parsed by PowerShell, it will throw an exception stating that no such cmdlet or function exists.

Function names can be any word or set of words; however, it is recommended to name the function similar to the verb-noun cmdlet naming syntax. To create a function, you need to use the word Function and declare a function name like display-text. You then need to enclose the repeatable commands in curly brackets after the function name.

The proper syntax of a function...