Creating Custom Functions with PowerShell

Custom functions in PowerShell enable developers to easily group sections of code together for easier and repeated usage.

Here is a simple HelloWorld function:

image

To execute this function, type the following:

HelloWorld

The easiest way to get started with functions is with PowerShell ISE and execute the code directly from the ISE. In case the ISE is not available, save the code to a ps1 file with any text editor, such as Notepad. Execute it from a PowerShell shell by typing

\HelloWorld.ps1

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.