TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

PowerShell Functions

PowerShell Functions with What is PowerShell, History of PowerShell, Features of PowerShell, PowerShell vs CMD, PowerShell Scripting, PowerShell Versions, PowerShell Commands, PowerShell Looping, PowerShell Conditions, PowerShell Array, PowerShell Brackets etc.

<< Back to POWERSHELL

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:

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:

  • 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:

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 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:

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 Functions

Example1: 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




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf