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 construction should return 10 (integer value).

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




powershell function return

As you can see, the 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).

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





powershell function return value

By default, PowerShell functions work in a such way that when executed, they write all the values that were displayed during the function’s operation to the $Result array. This is 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

I enjoy technology and developing websites. Since 2012 I'm running a few of my own websites, and share useful content on gadgets, PC administration and website promotion.
Cyril Kardashevsky

One comment

  1. Hi please help me with this code i am always getting a failed results.

    $files = Get-Content C:\temp\test\info.txt

    foreach($file in $files)

    $sourcePath = “C:\temp\test\source\” + $file
    $destinationpath = “C:\temp\test\destination”

    function check()
    Copy-Item $sourcePath $destinationpath



    if (check)


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.