PowerShell Functions

When we need to use the same code in more than one script, then we use a PowerShell function.

A function is a list of PowerShell statements whose name is assigned by the user. When we execute a function, we type the name of a function.

Like the cmdlets, functions can also have parameters. The function parameters can be read from the pipeline or from the command line.

In PowerShell, functions return the values that can be assigned to the variables or passed to the cmdlets or other functions. By using the return keyword, we can specify the return value.

Syntax

The following block describes a syntax for a function:

The above syntax includes the following terms:

  • A function keyword
  • A name which is given by you
  • A scope (It is optional)
  • Any number of named parameter
  • One or more commands of PowerShell which are enclosed in braces {}.

Scope of a function

  • In PowerShell, a function exists in a scope in which it was created.
  • If a function is in a script, it is only available to the statements within that script.
  • When a function is specified in the global scope, we can use it in other functions, scripts, and the command line.

Simple function

The following block describes you how to create the simplest function in a PowerShell:

To add the multiple statements to the function, we must use a semicolon to separate the statements or type each statement on a separate line.

To use the function, type the name of the function as given in the following block:

Example:

Type the following command in the PowerShell console to get the output of the above example:

Output:

Windows Operating System  Linux operating System  

Advanced function

Advanced functions are those functions which can perform operations that are similar to the operations performed with the cmdlets. These functions are used when a user wants to write a function without having to write a compiled cmdlet.

The main difference between using a compiled cmdlet and an advanced function is that the compiled cmdlets are the classes of .NET Framework that must be written in a .NET framework language. And, the advanced functions are written in the PowerShell script language.

The following example describes how to use the advanced function in PowerShell:

Type the following command in the PowerShell console to get the output of above example:

Output:

cmdlet Send-Greeting at command pipeline position 1  Supply values for the following parameters:  Name: Aman  Hi Aman!  

Examples of Functions

Example1: The following example is a simple function which returns a current date

Type the following command in the PowerShell console to get the output of above example:

Output:

15 November 2019 14:41:17  

Example2: The following example is a function which accepts one parameter and returns a value on that parameter.

Type the following command to get the input from a user for the above example:

Output:

Enter a value: 10  

Type the following command to store the return value from the function in a variable which displays the output of function:

The following command shows you a result:

Output:

10 * 10 = 100  

Next TopicTry Catch Finally

Previous articleLearn MS Word Tutorial
Next articleWhat is PowerShell cmdlet