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
Functions that return arrays

When you chain cmdlets together, the PowerShell pipeline wants/needs/is designed to feed "one thing at a time" down the pipeline, e.g.

Get-Content 'somefile' | Process-TextLine
Get-ChildItem | Process-Files

It doesn't matter what you do inside the function with @() and [array], it's the connection between functions where PowerShell will undo that and treat them as individual items again.

Which is usually what you want - it means you can use the pipeline, and | foreach-object {} and foreach ($thing in ____) {}, and if you just assign the results to a variable and there were multiple results, it will be an array.

The two main reasons you might want to force an array are:

  • The case the file was empty and you're getting $null but want an empty array. This also normally works fine for pipeline and loops, but storing in a variable doesn't work. That's your @() and [array] in the outer, calling, function where you store the results. This is (AFAIK) good practice to do it like this and not try to do it from inside the function.

  • The case where you want to group things together and send (3 lines) down the pipeline all together.

e.g.

PS D:\> @(1,2,3),@(4,5,6) | foreach  { "-$_-" }
-1 2 3-
-4 5 6-

And when there is only one of those, it falls over:

@(1,2,3) | ForEach { "-$_-" }
-1-
-2-
-3-

This is the case where you need u/TalkToTheFinger's solution of putting a comma in front, and all this does is force it to be like an array inside an array:

PS D:\> @(,@(1,2,3)) | ForEach { "-$_-" }
-1 2 3-

It looks a bit weird, like you should be able to write @(@(1,2,3)) or @(@(1,2,3), ) or @($null, @(1,2,3)) but they are all not quite the same, to get it right and make it work needs one comma in front.

The above behaviour still happens - the array is unpacked and each item is sent down the pipeline, it's just that each item is now "an array of 3 items".

There are two cases here - if you want [array]$thing = get-content 'filename' to force an array even if there's only one or no lines, do it that way.

If you try to force it with the comma, it will break in the normal case of lots of lines and they will all be sent together. i.e. it works to force an array in the case of 0 or 1 result, but it breaks in the normal case of lots of results - instead of "an array of results" you have "an array of array of results". If you want to write your own get-thing and you try to force it with ,$lines what you'll get is "an array of (an array of lines)" and that will be silly.

But sometimes you do want that "several arrays together, don't flatten them" behaviour, you can force it, and they will stay together down through the pipeline.

More on reddit.com
🌐 r/PowerShell
12
10
September 7, 2017
🌐
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.
🌐
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.
🌐
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.
Find elsewhere
🌐
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 27, 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.
🌐
Day3bits
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 - This function doesn't do anything other than return three arrays, but you get the idea. You can return any kind of object, or a mix of different object types. Cool! ... At this point, it's helpful to remember that everything in PowerShell is an object. (Yes, even strings are objects!)
🌐
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.
🌐
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
🌐
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
🌐
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....
🌐
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.
🌐
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, ...
🌐
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.