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 - $Value += 73 return $Value } $a = Calculation 14 · The "Please wait. Working on calculation..." string is not displayed. Instead, it is assigned to the $a variable, as in the following example: PS> $a Please wait. Working on calculation... 87 · Both the informational string and the result of the calculation are returned by the function and assigned to the $a variable. If you would like to display a message within your function, beginning in PowerShell 5.0, you can use the Information stream.
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 the function ... More on community.spiceworks.com
🌐 community.spiceworks.com
13
4
December 17, 2020
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
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
What are the best practices regarding return values from functions?
I would argue it to be against best practice but it is quite common for this sort of behaviour to be controlled by parameter sets usually with a switch. Think Group-Object which has a switch AsHashtable to return a hash table rather than a list of objects. So you could consider a switch Simple or AsBoolean or something to return only the Boolean. You could also consider having two functions. Maybe a Test-ValidUrl that returns the booleans. It’s fairly universal to expect Booleans from Test- functions. I would say that best practice regardless is to annotate the outputs using the OutputType decorator. It goes on the param block usually above the CMDletBinding. Official docs: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_outputtypeattribute More on reddit.com
🌐 r/PowerShell
32
26
November 20, 2021
🌐
SS64
ss64.com › ps › return.html
Return statement - PowerShell
In PowerShell, the result of each statement is returned as output, even without an explicit Return keyword. So $price return has exactly the same effect as: return $price ... function demoadd { param ($value) "Adding five" $value += 5 return $value } PS C:\> $result = demoadd 2 PS C:\> PS C:\> ...
🌐
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.
🌐
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 the function ...
🌐
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 - ... The [<expression>] can be any PowerShell object and data type, like Boolean, integer, string, or even a statement (such as return (24 + $abc)). In PowerShell, you can return values from a function even if you do not use the Return keyword.
🌐
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.
Find elsewhere
🌐
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.
🌐
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()&#xA;{ [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
🌐
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 - For example: function Get-Sum { param($a, $b) return $a + $b } Get-Sum -a 5 -b 10 # Output: 15 PowerShell automatically returns the last expression. ... PowerShell handles function output differently compared to traditional programming languages. Any value that’s sent to the pipeline inside a function becomes part of the function’s output.
🌐
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....
🌐
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).
🌐
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.
🌐
PowerShell Test-Path
powershellfaqs.com › powershell-functions-return-values
PowerShell Functions: Return Values and Multiple Values
July 6, 2024 - In this example, the function Get-Greeting takes a single parameter $Name and returns a greeting string. In PowerShell, you can return a value from a function either by using the return keyword or simply by outputting the value.
🌐
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 - PS C:\> Test-Return Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 811 28 64912 71076 0.78 10216 1 powershell This is a string. 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 other things.
🌐
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 - Function StartDoc { PARAM([string]$Orientation = "Portrait", [string]$TemplateFile) END { if ($Orientation.ToLower() -eq "landscape") {$o = 1} else {$o = 0} $word=new-object -ComObject "Word.Application" if ($TemplateFile -ne "") { if (-not (Test-Path $TemplateFile)) {Throw "Template not found $TemplateFile"} $document=$word.documents.Add($TemplateFile) } else { $document=$word.documents.Add() } $word.Visible=$True $document.PageSetup.Orientation = $o $selection=$word.Selection #Move to end of document $selection.EndOf(6) Return $selection } } The function worked perfectly and did what I wanted – but something weird was happening to that $selection object.
🌐
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!)
🌐
4sysops
4sysops.com › home › blog › popular › the powershell function – parameters, data types, return values
The PowerShell function – Parameters, data types, return values
July 28, 2023 - In general, the purpose of return is to end the execution of a code section and to give the control back to the parent block. If you add a parameter to the return statement, the value will indeed be returned to the calling subroutine.
🌐
Codewrecks
codewrecks.com › post › general › powershell › powershell-return-value
Return value in PowerShell, a typical error • Codewrecks
April 17, 2021 - If I dump the location of nunit executable in my main script, I can see that the value returned is not what I’m expecting. It is obvious that I’m looking at the output of Nuget.exe executables, and that was caused by missing of the nunit and nuget.exe tools in GH Action machine temp folder, so I run nuget install inside the function. The obvious solution is adding a Out-Null to avoid output to be returned · It is of uttermost importance that you pay attention on what goes in the output during a PowerShell function that is supposed to return something, because you can end spending 20 minutes on trivial errors like this ones.