C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
PowerShell FunctionsWhen 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. SyntaxThe following block describes a syntax for a function: function [<scope:>]<name> [([type]$parameter1[,[type]$parameter2])] { param([type]$parameter1 [,[type]$parameter2]) dynamicparam {<statement list>} begin {<statement list>} process {<statement list>} end {<statement list>} } The above syntax includes the following terms:
Scope of a function
Simple functionThe following block describes you how to create the simplest function in a PowerShell: function <function-name> { statement-1 statement-2 statement-N } 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: Function-name Example: PS C:\> function write-command >> { >> echo "Windows Operating System" >> echo "Linux operating System" >> } Type the following command in the PowerShell console to get the output of the above example: PS C:\> write-command Output: Windows Operating System Linux operating System Advanced functionAdvanced 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: PS C:\> function Send-Message >> { >> [CmdletBinding()] >> Param ( >> [ Parameter (Mandatory = $true)] >> [string] $Name >> ) >> >> Process >> { >> Write-Host ("Hi" + $Name + "!") >> } >> } Type the following command in the PowerShell console to get the output of above example: PS C:\> Send-Message Output: cmdlet Send-Greeting at command pipeline position 1 Supply values for the following parameters: Name: Aman Hi Aman! Examples of FunctionsExample1: The following example is a simple function which returns a current date PS C:\> function Get-DateTime() >> { >> return Get-Date >> } Type the following command in the PowerShell console to get the output of above example: PS C:\> Get-DateTime 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. PS C:\> function Get-Square([int]$x) >> { >> $res = $x * $x >> return $res >> } Type the following command to get the input from a user for the above example: PS C:\> $x = Read-Host 'Enter a value' 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: PS C:\> $sqres = Get-Square $x The following command shows you a result: PS C:\> Write-Output "$x * $x = $sqres" Output: 10 * 10 = 100
Next TopicTry Catch Finally
|