PowerShell Function Return

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

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

You may also like:

How to transfer FSMO Roles From a Failed Domain Co... In case domain controller, which owns FSMO (Flexible Single Master Operation) roles, is fail (virus attack, fatal software problems or catastrophic ha...
Time Configuration for a Virtualized Domain Contro... Today we will talk about some of the features of time configuration on a virtualized domain controllers. Typically, the time synchronization scheme in...
Removing Old and Unused Drivers from Driver Store ... Each time you install or update your device driver, Windows OS (since Vista) continues to store the old version of the driver in the system Driver Sto...
Installing WebDAV client on Windows Server 2016 WebDAV (Web Distributed Authoring and Versioning) is an extension of the HTTP/1.1 protocol. The WebDAV protocol allows to access files on the remote s...
Get-service: Checking the Status of Windows Servic... Using the Get-Service cmdlet you can get a list of all the services installed on the operating system, its status and startup type. This one and other...

Add Your Comment