PowerShell Functions

Simple Functions
 
Functions are reuseable code, which you can execute multiple times by calling the function's name. Functions are placed in the top of the script, because powerShell need to interpret the functions first before we use it. But if we don't it will throw an exception that no such cmdlet or functions exists. Basic syntax for function
The basic syntax of simple function.
 
Function function-name { Statement(s) }
 
It is recommended to name the function similar to the verb-noun cmdlet naming syntax. Example function to print text.
 
function Print-Text {
    Write-Host "This is print text function"
}

# calling the function
Print-Text
 
Example to run notepad as administrator.
 
function Start-NotepadAdmin {
    Start-Process notepad -Verb RunAs
}
Start-NotepadAdmin
 
Functions with Parameters
 
Functions allow you to pass in data for processing, the way to pass in data into function is declaring variables after the function name inside the parentheses. Basic syntax to pass in data in function.
 
Function function-name([type]$variable11, [type]$variable2) {
    Statement(s)
}
 
Example to get qube of number.
 
function Get-Qube([int]$value) {
    $result = $value * $value * $value
    return $result
}
Write-Host(Get-Qube 4)
 
Let's look more examples.
 
function Print-Text([string]$var1, [string]$var2) {
Write-Host "Var1 : $var1, Var2 : $var2"
}
Print-Text "Hello" "World"
 
Remember when you are calling functions with two or more arguments, don't pass the values within the brackets, example.
 
function Get-Qube([int]$value) {
    $result = $value * $value * $value
    return $result
}

Write-Host(Get-Qube(8)) # only one arguments, it's OK!

function Add-TwoNumber([int]$a, [int]$b) {
    return $a + $b
}
Add-TwoNumber 5 8 # Right
Add-TwoNumber(5, 8) # Wrong

function Sum-Numbers($list_num) {
    $sum = 0
    foreach($num in $list_num) {
        $sum += $num
    }
    return $sum
}

$res = Sum-Numbers(1, 2, 3, 4, 5, 6) # this is an object, it's OK!
Write-Host $res
 
Begin, Process, End Block
 
Any function can take input from the pipeline. You can control how a function processes input from the pipeline using Begin, Process, and End statement. This is the sample syntax of the three blocks.
 
Function function-name() {
    Begin { statement(s) }
    Process { statement(s) }
    End { statement(s) }
}
 
The Begin statement runs one time only, at the beginning of the function. The Process statement list runs one time for each object in the pipeline which is assigned in $_ automatic variable at a time. 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. Example function with process statement.
 
function Get-ProcessBlock {
    Process {
        Write-Host "The process is : $_"
    }
}
1, 2, 3 | Get-ProcessBlock
 
The script will show result like this.
 
PS D:\> .\script.ps1
The process is : 1
The process is : 2
The process is : 3
 
When you use a function in a pipeline, the objects piped to the function are assigned to the $input automatic variable if the function only using begin and end statement. The function runs begin statement statement before any objects come from the pipeline and end statement after all the objects have been received from the pipeline. Example 
 
function Get-BeginEnd {
    begin {
        Write-Host "begin: The input is $input"
    }
    end {
        Write-Host "end: The input is $input"
    }
}
1,2,3 | Get-BeginEnd
 
The script will show result like this.
 
PS D:\> .\script.ps1
begin: The input is
end: The input is 1 2 3
 
When the function has a process statement, the function will read data from $input, example
 
function Get-ReadInput {
    Process {
        Write-Host "Process: $_"
    }
    End {
        Write-Host "End: $input"
    }
}
1, 2, 3, 4 | Get-ReadInput
 
The script will show result like this.
 
PS D:\> .\script.ps1
Process: 1
Process: 2
Process: 3
Process: 4
End: 
 
In the example above, each object that is piped to the function is sent to the Process statement. Then Process statements run on each object, one object at a time. The $input automatic variable is empty when the function reaches the End statement.

Subscribe to receive free email updates:

0 Response to "PowerShell Functions "

Post a Comment