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

🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.core › about › about_return
about_Return - PowerShell | Microsoft Learn
January 18, 2026 - The return keyword exits a function, script, or scriptblock. It can be used to exit a scope at a specific point, to return a value, or to indicate that the end of the scope has been reached.
Discussions

Powershell function return value
So I’ve got a functions file ... in other powershell scripts that I run as scheduled tasks. I have created a new function that does some invoke-webrequests. I am storing the webrequests in variables and I am trying to post the data in these variables back to the other script that I am calling the functions from. I am using return values in ... More on community.spiceworks.com
🌐 community.spiceworks.com
13
4
December 17, 2020
Explaining Rationale for Function Output Behavior
Ok, so I discovered (the hard way) how functions sort of queue up any output from within the function and then return all of it as part of the return object. function foo () { echo "SettingVar" $var = "varValue" e… More on forums.powershell.org
🌐 forums.powershell.org
8
0
October 3, 2016
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
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
🌐
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 - 10 · As described in about_Return, the Return keyword exits the current scope. That's why the command after the Return statement ("Here is another string") doesn't run. Return also returns the associated object (here, it's the integer 10), but it doesn't prevent the function from returning ...
🌐
Spiceworks
community.spiceworks.com › programming & development
Powershell function return value - Programming & Development - Spiceworks Community
December 17, 2020 - So I’ve got a functions file ... in other powershell scripts that I run as scheduled tasks. I have created a new function that does some invoke-webrequests. I am storing the webrequests in variables and I am trying to post the data in these variables back to the other script that I am calling the functions from. I am using return values in ...
🌐
PowerShell Forums
forums.powershell.org › powershell help
Explaining Rationale for Function Output Behavior - PowerShell Help - PowerShell Forums
October 3, 2016 - Ok, so I discovered (the hard way) how functions sort of queue up any output from within the function and then return all of it as part of the return object. function foo () { echo "SettingVar" $var = "varValue" echo "VarSet" return $var } > foo SettingVar varValue VarSet I initially found this to be idiotic.
🌐
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.
🌐
Powershellstation
powershellstation.com › 2011 › 08 › 26 › powershell’s-problem-with-return
PowerShell’s Problem with Return – PowerShell Station
August 26, 2011 - In most languages, if you see “return 1″ as the only return in a function, you can know that the function is going to the value 1 to the caller. In fact, I’m not sure I’ve ever seen a language that didn’t work that way. That is, until I found PowerShell.
Find elsewhere
🌐
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 - Because we can return values from a PowerShell function, the value of the return keyword might not immediately be evident. The difference between returning values with Write-Output and the return keyword is using the return keyword exits the current scope.
🌐
TheITBros
theitbros.com › powershell › using return statement in powershell functions and scripts – theitbros
Using Return Statement in PowerShell Functions and Scripts – TheITBros
January 5, 2026 - Function GetServiceAndProcess { return ` $( Get-Service -ErrorAction SilentlyContinue | Select-Object -First 5 ), $( Get-Process -ErrorAction SilentlyContinue | Select-Object -First 5 ) } Invoke the PowerShell function and save the results in the $result variable.
🌐
Day 3 Bits
day3bits.com › 2023-12-08-return-multiple-objects-from-a-powershell-function
Return Multiple Objects from a PowerShell Function - Day 3 Bits
December 8, 2023 - If you run this code, you’ll notice something that could be an issue: the objects that are returned in $Food look like an ambiguous list. Apple Orange Peach Pumpkin Acorn Squash Winter Squash Potato Sweet Potato Turnip Radish · The array names are gone, so if you want to reference a specific type of food from the results, you have to find a creative way to get it. What if you need to enumerate the TreeFruit array, and then enumerate the RootVegetable array later in a different function?
🌐
SAPIEN Technologies
sapien.com › home › windows powershell › powershell functions: return vs write
PowerShell Functions: Return vs Write - SAPIEN Blog
June 2, 2009 - I was a fan of return, but I’m ... as to the ambiguity of the return statement though.. it returns to the value to the place where the function was called from....
🌐
SPGuides
spguides.com › powershell-function-return-values
PowerShell Function Return Values
January 19, 2024 - In this example, the function Get-MultipliedValue takes two parameters and returns their product. Returning a string from a PowerShell function is straightforward.
🌐
Davemason
davemason.me › 2021 › 11 › 29 › powershell-functions-and-return-types
PowerShell Functions and Return Types - It's All Just Electrons
November 29, 2021 - This is definitely top 3 of the most idiotic things the Powershell devs did. c# doesn't try and override your return values, whoever designed this should be off of the Powershell project. Brad · Here is a work around stackoverflow.com/.../function-return-value-in-powershell& Get-RandomDate()
{ [OutputType([System.DateTime])] Param ( [parameter(Mandatory=$true)] [System.Int32] $DaysAgo ) { [System.DateTime]$ret = [System.DateTime]::Today $randomDays = Get-Random -Minimum 0 -Maximum $DaysAgo $ret = [System.DateTime]::Today.AddDays($randomDays * -1) [System.Text.StringBuilder] $sb = New-Object System.Text.Stringbuilder $sb.Append("Hello ") $sb.Append("world!") } | Out-Null; return $ret } Brad
🌐
DevTut
devtut.github.io › powershell › return-behavior-in-powershell.html
PowerShell - Return behavior in PowerShell
The Return keyword exits a function, script, or script block. It can be used to exit a scope at a specific point, to return a value, or to indicate that the end of the scope has been reached.
🌐
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.
🌐
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.
🌐
i Love PowerShell
ilovepowershell.com › home › powershell functions: creating, calling, and returning
PowerShell Functions: Creating, Calling, and Returning
March 17, 2023 - The function then calculates the sum of the two numbers and stores it in a variable called $Sum. Finally, the function returns the value of $Sum using the Return keyword. Functions are a powerful feature of PowerShell that can help you to write more modular, reusable, and maintainable code.
🌐
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?
🌐
SharePoint Diary
sharepointdiary.com › sharepoint diary › powershell › powershell functions: a comprehensive beginner’s guide
PowerShell Functions: A Comprehensive Beginner's Guide - SharePoint Diary
September 29, 2025 - For example, the following function returns the value of the $result variable: ... You can use the return statement to specify the value that a PowerShell function returns. You can use it to return any data type, including strings, integers, ...