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
Is the RETURN statement necessary in a Function?
Just about every blog or post I’ve run into says the ‘Return’ statement in PS is really not needed, that all it does is to stop execution and pass control back to the calling routine. More relevant to this is that “everything that is outputted in the called Function is returned to the ... More on forums.powershell.org
🌐 forums.powershell.org
2
0
August 6, 2018
Write-Output vs. Return vs. $Output
There is currently a recommendation in Function and Structure to not use "return" but it says to "just put the variable on a line by itself" ... I wonder if that's the major... More on github.com
🌐 github.com
35
August 14, 2015
🌐
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.
🌐
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.
🌐
SAPIEN Technologies
sapien.com › home › windows powershell › powershell functions: return vs write
PowerShell Functions: Return vs Write - SAPIEN Blog
June 2, 2009 - You can also use Return to send an array to the pipeline which also gets around the limitation. But why bother? Why not simply use Write-Object all the time and avoid the confusion. Even in a function that can only return a single value, like my temperature conversion example, using Write-Object makes it very clear what PowerShell is going to do.
Find elsewhere
🌐
Outcompute
outcompute.com › 2014 › 11 › 27 › the-difference-between-break-return-and-exit-in-powershell
The Different Between Break, Return & Exit In PowerShell
November 27, 2014 - We have replaced the Break statement with a Return statement. Running the code now produces this: Calling the function... Making pumpkins... Pumpkin 1 Pumpkin 2 Finished running the function! Do you notice the difference? We no longer see that output at the end of the loop. Yet, we do see that execution continued right after the function call. This is quite the oddball. The Exit statement isn’t really a CmdLet to begin with. The Exit statement is an internal command of PowerShell that instructs it to terminate the session altogether.
🌐
missmiis
wapshere.com › missmiis › returning-an-object-from-a-powershell-function
Returning an object from a PowerShell function – missmiis
March 8, 2013 - It turns out that “Return” command is a bit misleading. Yes the function returns that value, but it also returns anything else in the function that produces output.
🌐
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
🌐
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…
🌐
Powershellstation
powershellstation.com › 2011 › 08 › 26 › powershell’s-problem-with-return
PowerShell’s Problem with Return | PowerShell Station
August 27, 2011 - In the absence of any statements writing to the output stream (with write-output) or “dropping” their values, “return 1″ will cause the caller to receive the value “1″. Using write-output is pretty obvious, and I’d recommend using it explicitly if you intend to add objects to the output stream (thereby including them in the eventual function value).
🌐
Davemason
davemason.me › 2021 › 11 › 29 › powershell-functions-and-return-types
PowerShell Functions and Return Types - It's All Just Electrons
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.
🌐
PowerShell Forums
forums.powershell.org › powershell help
Is the RETURN statement necessary in a Function? - PowerShell Help - PowerShell Forums
August 6, 2018 - Just about every blog or post I’ve run into says the ‘Return’ statement in PS is really not needed, that all it does is to stop execution and pass control back to the calling routine. More relevant to this is that “everything that is outputted in the called Function is returned to the calling routine: as an array.” Q1 - WHERE is the array?
🌐
GitHub
github.com › PoshCode › PowerShellPracticeAndStyle › issues › 46
Write-Output vs. Return vs. $Output · Issue #46 · PoshCode/PowerShellPracticeAndStyle
August 14, 2015 - There is currently a recommendation in Function and Structure to not use "return" but it says to "just put the variable on a line by itself" ... I wonder if that's the majority opinion? Assuming this is the last line of my function, what...
Author   PoshCode
🌐
SOC Prime
socprime.com › active-threats › lummastealer-using-castleloader
LummaStealer Returns: CastleLoader AutoIt Delivery Chain
February 17, 2026 - LummaStealer resurfaces after 2025 takedowns, now delivered by AutoIt-based CastleLoader via fake cracks/CAPTCHAs, with obfuscation and DNS-based IOCs
🌐
Chocolatey Software
docs.chocolatey.org › en-us › choco › setup
Chocolatey Software Docs | Setup / Install
This # will typically produce a message for PowerShell v2 (just an info # message though) try { # Set TLS 1.2 (3072), then TLS 1.1 (768), then TLS 1.0 (192), finally SSL 3.0 (48) # Use integers because the enumeration values for TLS 1.2 and TLS 1.1 won't # exist in .NET 4.0, even though they are addressable if .NET 4.5+ is # installed (.NET 4.5 is an in-place upgrade).
🌐
Stanford Alumni Association
alumni.stanford.edu
Welcome to the Stanford Alumni Association | Stanford Alumni Association
Experience a cruise like none other and see a totally different side of the cities, towns and scenic seascapes along the way. After a day on the water, you’ll return to the comfort and safety of intimate vessels chartered exclusively for Stanford.
🌐
Express.js
expressjs.com › en › starter › installing
Installing · Express.js
This command prompts you for a number of things, such as the name and version of your application. For now, you can simply hit RETURN to accept the defaults for most of them, with the following exception:
🌐
James O'Neill's Blog
jamesone111.wordpress.com › 2019 › 08 › 21 › exit-throw-return-break-and-continue-a-round-up
Exit, Throw, Return, Break and Continue. A Round up. | James O'Neill's Blog
August 26, 2019 - $Result = complex | pipeline -of "commands" return $result · And then there is Exit. Exit says “Leave what you are running” at the PowerShell prompt it is Leave PowerShell, in a script it is Leave the script. Exit can return an exit code.