PowerShell Functions: Return vs Write

The other day I was involved in a short Twitter debate about the relative merits of using Return vs Write-Object (or its alias Write) in PowerShell functions. Here’s my take.

I treat Return as a legacy keyword. You may have used this often in functions written in other languages. In a PowerShell function however, it will return the specified value to the pipeline and then exit the function. This is not a big deal if your function can only return one value anyway such as a function to convert Fahrenheit to Celsius. However, if the code in your function will return multiple objects, you won’t get them all using the Return keyword.  Here’s an example.

Function Test-Return {
Write-Host "This function will use the RETURN keyword" -foregroundcolor CYAN
$data=Get-Service | where {$_.status -eq "stopped"}
#this returns a single item
 foreach ($item in $data) { Return $item.displayname}

If you run this code you will only get a single object returned. Compared this with a function that uses Write.

Function Test-Write {
Write-Host "This function will use the WRITE keyword" -foregroundcolor CYAN
$data=Get-Service | where {$_.status -eq "stopped"}
foreach ($item in $data) {write $item.displayname}

This will send all objects to the pipeline.

I’m certainly aware that there are ways to write a function so that Return will send all objects to the pipeline. 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. Return can be a little ambiguous. Return what to where?

I occasionally use Return in a function when I specifically want its behavior, but I consider this more an exception rather than the rule.

My recommendation is to relegate Return to the VBScript and text based shells or scripting languages of the past and embrace Write-Object and the PowerShell paradigm.

Oh, and if you want to follow the PowerShell discussions on Twitter, I’m at http://twitter.com/JeffHicks and you’ll find a lot of people to follow, including other PowerShell MVPs at http://wefollow.com/tag/powershell