PowerShell has really wacky return semantics - at least when viewed from a more traditional programming perspective. There are two main ideas to wrap your head around:

  • All output is captured, and returned
  • The return keyword really just indicates a logical exit point

Thus, the following two script blocks will do effectively the exact same thing:

$a = "Hello, World"
return $a

 

$a = "Hello, World"
$a
return

The $a variable in the second example is left as output on the pipeline and, as mentioned, all output is returned. In fact, in the second example you could omit the return entirely and you would get the same behavior (the return would be implied as the function naturally completes and exits).

Without more of your function definition I can't say why you are getting a PSMethod object. My guess is that you probably have something a few lines up that is not being captured and is being placed on the output pipeline.

It is also worth noting that you probably don't need those semicolons - unless you are nesting multiple expressions on a single line.

You can read more about the return semantics on the about_Return page on TechNet, or by invoking the help return command from PowerShell itself.

Answer from Goyuix on Stack Overflow
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.core › about › about_return
about_Return - PowerShell | Microsoft Learn
January 18, 2026 - Languages like C or C# return only the value or values that are specified by the return keyword. ... Beginning in PowerShell 5.0, PowerShell added language for defining classes, by using formal syntax. In the context of a PowerShell class, nothing is output from a method except what you specify using a return statement.
Top answer
1 of 10
358

PowerShell has really wacky return semantics - at least when viewed from a more traditional programming perspective. There are two main ideas to wrap your head around:

  • All output is captured, and returned
  • The return keyword really just indicates a logical exit point

Thus, the following two script blocks will do effectively the exact same thing:

$a = "Hello, World"
return $a

 

$a = "Hello, World"
$a
return

The $a variable in the second example is left as output on the pipeline and, as mentioned, all output is returned. In fact, in the second example you could omit the return entirely and you would get the same behavior (the return would be implied as the function naturally completes and exits).

Without more of your function definition I can't say why you are getting a PSMethod object. My guess is that you probably have something a few lines up that is not being captured and is being placed on the output pipeline.

It is also worth noting that you probably don't need those semicolons - unless you are nesting multiple expressions on a single line.

You can read more about the return semantics on the about_Return page on TechNet, or by invoking the help return command from PowerShell itself.

2 of 10
77

This part of PowerShell is probably the most stupid aspect. Any extraneous output generated during a function will pollute the result. Sometimes there isn't any output, and then under some conditions there is some other unplanned output, in addition to your planned return value.

So, I remove the assignment from the original function call, so the output ends up on the screen, and then step through until something I didn't plan for pops out in the debugger window (using the PowerShell ISE).

Even things like reserving variables in outer scopes cause output, like [boolean]$isEnabled which will annoyingly spit a False out unless you make it [boolean]$isEnabled = $false.

Another good one is $someCollection.Add("thing") which spits out the new collection count.

Discussions

Why not use the return statement?
First off I do want to say that you are not in the wrong here. You are using it for the right reasons. The recomendation from me would be to use Write-Output instead unless the situation specifically needs a return statement. That is more what the common convention is. All the reasons you state also apply to Write-Output. Not dropping objects on the pipe and making your intentions clear are exactly the things you should be thinking about. I beleive the reason we are pushed to use Write-Output is so that we are more aware of how we can use the pipe. I step back on all my functions and consider how it works with a set of data and not just a single object (even if the purpose at the time is for single object processing). Because of that, it is very rare that I ever use a return statment. You will walk away with some really great tools that may not show their value until much later More on reddit.com
🌐 r/PowerShell
13
12
May 18, 2016
Return object from called Powershell script
main.ps1 $rc = Start-Process -FilePath ... $rc = 1 $partialFile = ‘20230130_1938’ $ostGo = $true return New-Object PSObject -Property @{ rc = $rc; file = $partialFile; ostDecide = $ostGo } Trying to get Powershell in script1 to pass back this object.... More on forums.powershell.org
🌐 forums.powershell.org
7
0
February 2, 2023
🌐
SAPIEN
info.sapien.com › index.php › scripting › scripting-classes › using-the-return-keyword-in-powershell-classes
Using the Return Keyword in PowerShell Classes - SAPIEN Information Center | SAPIEN Information Center
October 9, 2019 - To test, I'll create a ReturnTester object and save it in the $rt variable. ... Now, I'll run the TestReturn method. ... In a PowerShell class method, Return exits and current scope and returns ONLY the associated object.
🌐
SS64
ss64.com › ps › return.html
Return statement - PowerShell
function demoadd { param ($value) "Adding five" $value += 5 return $value } PS C:\> $result = demoadd 2 PS C:\> PS C:\> $result Adding five 7
🌐
TechTarget
techtarget.com › searchwindowsserver › tutorial › Cut-coding-corners-with-return-values-in-PowerShell-functions
Cut coding corners with return values in PowerShell functions | TechTarget
March 29, 2021 - The distinction between the return's implementation between C and in PowerShell is that while C only returns values passed to the return keyword, PowerShell can return values to the pipeline without a return.
🌐
Davemason
davemason.me › 2021 › 11 › 29 › powershell-functions-and-return-types
PowerShell Functions and Return Types - It's All Just Electrons
November 29, 2021 - As a result of some struggles trying to automate a process, I've learned some things about PowerShell. After getting to the bottom of a time-consuming problem, I thought it worth a blog post that might save someone else some time and heartache. Let's begin with this simple function named Get-RandomDate. It generates and returns a random date that is between today and X days ago.
🌐
TheITBros
theitbros.com › home › windows › powershell › using return statement in powershell functions and scripts
How to Use the Return Statement in PowerShell Functions and Scripts
January 5, 2026 - Those who dabbled in programming would be familiar with the return keyword. The primary use of return in PowerShell is to return the result of a function and/or exit the current scope. The scope can be a function, script, or script block.
Find elsewhere
🌐
DevTut
devtut.github.io › powershell › return-behavior-in-powershell.html
PowerShell - Return behavior in PowerShell
It can be used to Exit the current scope, which can be a function, script, or script block. In PowerShell, the result of each statement is returned as output, even without an explicit Return keyword or to indicate that the end of the scope has been reached.
🌐
Rip Tutorial
riptutorial.com › return behavior in powershell
PowerShell Tutorial => Return behavior in PowerShell
It can be used to Exit the current scope, which can be a function, script, or script block. In PowerShell, the result of each statement is returned as output, even without an explicit Return keyword or to indicate that the end of the scope has been reached.
🌐
PowerShell Test-Path
powershellfaqs.com › powershell-functions-return-values
PowerShell Functions: Return Values and Multiple Values
July 6, 2024 - If you are new to functions in PowerShell, then here is the syntax for declaring a function. function Get-Greeting { param ( [string]$Name ) "Hello, $Name!" } In this example, the function Get-Greeting takes a single parameter $Name and returns a greeting string.
🌐
Reddit
reddit.com › r/powershell › why not use the return statement?
r/PowerShell on Reddit: Why not use the return statement?
May 18, 2016 -

Coming from a managed code background, I use return in just about every function I write. When I want to return a collection to the pipeline, I add a # return comment so I know that I explicitly meant to return the object. For the community and my coworkers... I am on the wrong side of this battle. I'll continually receive code reviews saying I should not use the return statement, but I think thats their choice. I use it for the purpose of being explicit. I've helped the same people who want me to stop using it debug their broken code because they accidentally were returning something else into their pipeline that they didn't want there. Not just once, but at least once a sprint. Each.

So, grand PowerShell community, what's up with the pushback on return?

Top answer
1 of 5
7
First off I do want to say that you are not in the wrong here. You are using it for the right reasons. The recomendation from me would be to use Write-Output instead unless the situation specifically needs a return statement. That is more what the common convention is. All the reasons you state also apply to Write-Output. Not dropping objects on the pipe and making your intentions clear are exactly the things you should be thinking about. I beleive the reason we are pushed to use Write-Output is so that we are more aware of how we can use the pipe. I step back on all my functions and consider how it works with a set of data and not just a single object (even if the purpose at the time is for single object processing). Because of that, it is very rare that I ever use a return statment. You will walk away with some really great tools that may not show their value until much later
2 of 5
3
I've helped the same people who want me to stop using it debug their broken code because they accidentally were returning something else into their pipeline that they didn't want there. How does the return statement help here? Just because you use "return" doesn't make everything else you output to the pipeline not get outputted. "return" with a parameter doesn't make any sense in PowerShell, and allowing it was a mistake IMO (on the other hand "return" without a parameter, for returning from a function early, is perfectly fine). PowerShell functions don't return values at the end of functions' execution, they write any number of values to the pipeline, and can do so at any point during the execution of a function. This is hugely different to other imperative languages, and those languages use the return $value syntax or similar. In PowerShell, return $value actually means $value; return (or Write-Output $value; return), but it looks very similar to returning a single value in those other, common imperative languages, and thus has high potential for causing confusion and misunderstanding of how PowerShell works with object streams and the pipeline.
🌐
Mike Frobbins
mikefrobbins.com › 2015 › 07 › 23 › the-powershell-return-keyword
The PowerShell return keyword | mikefrobbins.com
July 23, 2015 - The return keyword is probably the most over used keyword in PowerShell that’s used in unnecessary scenarios. You’ll often find it used to simply return the …
🌐
PowerShell Forums
forums.powershell.org › powershell help
Return object from called Powershell script - PowerShell Help - PowerShell Forums
February 2, 2023 - main.ps1 $rc = Start-Process -FilePath powershell.exe -Argumentlist ‘-File c:\temp\script1.ps1’ -WorkingDirectory c:\Temp -Wait -PassThru $rc script1.ps1 $rc = 1 $partialFile = ‘20230130_1938’ $ostGo = $true retur…
🌐
Codewrecks
codewrecks.com › post › general › powershell › powershell-return-value
Return value in PowerShell, a typical error • Codewrecks
April 17, 2021 - This means that if you end your function with return $something you would not get only the content of the variable $something but every output that you did in the function.
🌐
SharePoint Diary
sharepointdiary.com › sharepoint diary › powershell › how to return value from a function in powershell?
How to Return Value from a Function in PowerShell? - SharePoint Diary
March 26, 2026 - Unlike many languages, PowerShell doesn’t require you to use return explicitly to send output. If a function processes data and sends it to the pipeline without return, the output still becomes the function’s return value.
🌐
SPGuides
spguides.com › powershell-function-return-values
PowerShell Function Return Values
January 19, 2024 - In PowerShell, a function can return a value using the return keyword. However, it’s important to note that PowerShell functions also return values implicitly.
🌐
4sysops
4sysops.com › home › blog › powershell tutoiral › the powershell function – parameters, data types, return values
The PowerShell function – Parameters, data types, return values
July 28, 2023 - JavaScript and PHP have the return keyword for this purpose; in VBScript, you use a variable with the same name as the one of the function. PowerShell also knows the return keyword; however, it follows a different logic.
🌐
Powershell Commands
powershellcommands.com › powershell-return-statement
Understanding PowerShell Return Statement with Examples
June 19, 2024 - Mastering the PowerShell Or Statement: A Quick Guide · What is the Return Statement in PowerShell? The return statement in PowerShell is a method used to send data from within a function back to the caller.