Quantcast
Channel: Powershell – TheITBros
Viewing all articles
Browse latest Browse all 91

PowerShell: Function Return

$
0
0
PowerShell Function Return

Most PowerShell newbies believe that PowerShell functions can return a value only through the Return statement. The return statement usually terminates the function and returns control to the calling function. But in Windows PowerShell, this is not entirely true…

In this article, we will look at how to return values from PowerShell functions using the Return command.

In traditional programming languages, functions usually return a single value of a particular type, but in Windows PowerShell, the results of the function are sent to the output stream.

Let’s look at the simplest PowerShell function:

function TestReturn($param) {

 $x = $param + 5

 "text"

 2*2

 return $x

}

If you run this function with a parameter 5 (TestReturn (5)) in a classical programming language, such a construction should return 10 (integer value).

However, in the case of PowerShell, we will see the output:

text

4

10

powershell function return

As you can see, PowerShell function returned 3 values as a result.

Note. It is not necessary to specify a Return command in a PowerShell function., The value of any variable or object that is displayed directly in the body of the function will be available as the function output.

Assign the value of the function to a variable and check its type with the following commands:

$MyTestVar=TestReturn (5)

$MyTestVar.GetType().FullName

The value returned by the function is of type System.Object [], i.e. this is an array. If the output of the function contains objects, the returned object instances will also fall into the collection (dynamic array).

You can display the length of the array and the values contained in it:

$MyTestVar.length

$MyTestVar[0]

$MyTestVar[1]

$MyTestVar[2]

powershell function return value

By default, PowerShell functions work so that when executed, they write to the $Result array all the values that were displayed during the function’s operation. This not always convenient when writing PowerShell functions.

In order not to send extra values to the output stream of the function, you must use the Write-Host cmdlet. This cmdlet will output function data to the console (on the screen), and not to the output stream:

function TestReturn($param) {

 $x = $param + 5

 Write-Host "text"

 Write-Host 2*2

 return $x

}

$MyTestVar = TestReturn (5)

$MyTestVar

As you can see, the function returned only the value that we expected – 10.

powershell function return type

You can also hide extra information from the output stream using the Return statement (it resembles the Break command in PowerShell loops).

function TestReturn($param) {

 “This line is displayed”

 return 

 “ This line is not displayed”

}

$MyTestVar = TestReturn (5)

$MyTestVar

powershell function return multiple values

The post PowerShell: Function Return appeared first on TheITBros.


Viewing all articles
Browse latest Browse all 91

Trending Articles