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



 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:




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)


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).

READ ALSO  Using Powershell cmdlet Invoke-Command

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





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)


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”


 “ This line is not displayed”


$MyTestVar = TestReturn (5)


powershell function return multiple values

Cyril Kardashevsky
Latest posts by Cyril Kardashevsky (see all)
READ ALSO  Manage and Disable Windows Defender Using PowerShell

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.