Creating and Using Functions

Windows PowerShell functions are named sets of commands that can accept input from the pipeline. When you call a function by its name, the related commands run just as if you had typed them at the command line. Normally, you save functions that you want to use frequently by typing the related commands into a profile file. You also can add functions to scripts.

Creating Functions

To create a function, type the word function followed by a name for the function. Type your command text, and enclose it in braces ({ }). For example, the following command creates the getwinrm function. This function represents the "get-service -name winrm" command:

function getwinrm {get-service -name winrm}

The braces ({ }) create a code block ...

Get Windows PowerShell™ 2.0: Administrator’s Pocket Consultant now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.