Functions and Parameters

When creating a PowerShell function, you can define a number of parameters that need to be passed when a function is called from the code; the simplest version of a PowerShell function with parameters is the following:

Function HelloWorld($YourName)

For more flexibility with parameter properties, use the param syntax:

image

With the preceding syntax function, an author can define which parameters are required, variable types for each variable, and default values. Functions with parameters can be called as shown next. When passed, parameters are separated with spaces only; commas are not used:

HelloWorld "John Smith" 47HelloWorld ...

Get Microsoft SharePoint 2010 Unleashed 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.