You are currently viewing Functions In PowerShell

Functions In PowerShell

Functions In PowerShell

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed the Array and Hash tables in PowerShell.

https://cloudaffaire.com/array-and-hash-tables-in-powershell/

In this blog post, we will discuss Function in PowerShell. Like any other programming language, PowerShell also supports the use of functions. A function is in a nutshell is the grouping of multiple commands together that can be reused. The main benefit of function is its reusability, you have to write the function only once and then you can reuse it as many times as you wish.

You can input certain values in the function in the form of parameters. A parameter a value that is passed to the function as input during the function call. Apart from the main process block, PowerShell function also supports the use of begin and end block. By default, the scope of the function is limited to the function itself. Below is the syntax of a PowerShell function.

Functions In PowerShell:

Create A Function In PowerShell:

You can create a new function using function keyword followed by function name and function definition. The function name should not contain any keywords or space. Once the function is created, you can execute the function by simply calling the function name.

Create A Function With Parameter In PowerShell:

PowerShell function accepts parameters. A parameter is an input that can be passed to the function during the function call. In order to use parameters in your function, you have to define the parameter that the function will accept in the function definition. Parameters can be defined with function name using braces or with function body using param keyword.

Create A Function With Default Parameter Value In PowerShell:

You can define a default value to the function parameter. If no parameter is passed during the function call, the default value will be used.

 

Create A Function With Mandatory Parameter In PowerShell:

You can use a Mandatory keyword with function parameter definition to make the parameter mandatory (must be passed during function call). In interactive mode, PowerShell will ask for the mandatory parameter during function call if not provided already and in non-interactive mode, PowerShell will throw an error if the mandatory parameter is not passed.

 

Create A Function With Specific Parameter Datatype In PowerShell:

You can define the data type of the parameter in PowerShell function. The function will throw an error if specified datatype is not matched during the function call.

Create A Function With Positional Argument In PowerShell:

You can use the “Position” keyword to define the position of the parameter. The 1st argument to the function will be position 0. If you want to interchange the position of the argument, use the parameter name while passing the argument.

Create A Function With Input Processing Blocks In PowerShell:

Any function can take input from the pipeline. You can control how a function processes input from the pipeline using Begin, Process, and End keywords.

  • Begin: The Begin statement list runs one time only, at the beginning of the function.
  • Process: The Process statement list runs one time for each object in the pipeline. While the Process block is running, each pipeline object is assigned to the $_ automatic variable, one pipeline object at a time.
  • End: After the function receives all the objects in the pipeline, the End statement list runs one time. If no Begin, Process, or End keywords are used, all the statements are treated like an End statement list.

Hope you have enjoyed this article. In the next blog post, we will discuss the execution policy in PowerShell.

To get more details on PowerShell, kindly follow below official documentation

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about?view=powershell-5.1