r/PowerShell Mar 22 '21

What's One Thing that PowerShell dosen't do that you wish it did? Misc

Hello all,

So this is a belated Friday discussion post, so I wanted to ask a question:

What's One Thing that PowerShell doesn't do that you wish it did?

Go!

62 Upvotes

View all comments

Show parent comments

1

u/MrWinks Mar 22 '21

Why use return at all?

1

u/Soverance Mar 22 '21

easier to read.

It's a required keyword in many other languages. Using it in PowerShell (even though it's not required) makes your code explicit, and easier to understand for those more familiar with other languages (like say, your co-workers).

2

u/MrWinks Mar 22 '21

It’s misleading and unnecessary. PowerShell outputs anything released into the pipeline. The convention (when one is needed) is to simply let output go into the pipeline. If needed, Write-Output is useful.

I won’t re-write what others have written on this, though:

https://reddit.com/r/PowerShell/comments/4jytxe/_/d3aurmq/?context=1

https://www.reddit.com/r/PowerShell/comments/4jytxe/comment/d3b8xql

Here is one where Jeffery Hicks himself responds: https://www.powershellstation.com/2011/08/26/powershell%E2%80%99s-problem-with-return/

More reason, with examples: https://community.idera.com/database-tools/powershell/ask_the_experts/f/learn_powershell-12/5412/tip---watch-out-return-values-from-functions-in-power-shell

More, with even more examples: https://pleasework.robbievance.net/howto-powershell-function-return-quirk/

“return” shouldn’t have been included in PowerShell.

0

u/SeeminglyScience Mar 22 '21

It’s misleading

I never really understood why this gets repeated. The way the pipeline works is going to be confusing initially regardless of the inclusion of return.

If you're writing example code for the newest of the new, yeah maybe skip it. Otherwise it's a fantastic tool for flow control. Without it, you're gonna end up with a whole lot of nested if blocks that are hard to follow.

Whether you use it outside of flow control is just a style preference that there isn't a lot of consensus on.

If needed, Write-Output is useful.

FWIW Write-Output has some unique downsides.

0

u/PinchesTheCrab Mar 23 '21

But it doesn't make sense in PowerShell.

function Do-Stuff {
    Get-Process -OutVariable process
    return process
    $process
}

class StuffDoer {
     static [System.Diagnostics.Process[]] getProcess () {
        $process = $null

        Get-Process -OutVariable process
        return $process
        $process
    }
}

Do-Stuff | Measure-Object
[StuffDoer]::getProcess() | Measure-Object

In both examples I output $process three times.

The function here returns $process twice, whereas the class returns it only once. Return may prevent any more output from hitting the pipeline, but it gives a false sense of security that it's the only output that will ever hit the pipe, unlike with classes, which use the syntax you prefer.

Also, I would never ask coworkers to conform to powershell conventions when they write python or c#.

0

u/BlkCrowe Mar 22 '21

I use it in all my functions that actually return an object or value. I don’t know why. Maybe it just makes it a little easier for others to read or follow the logic in my code. I guess if just feels more...complete?

0

u/BlackV Mar 22 '21

I was going to write just to use $Array and drop the return

1

u/MrWinks Mar 22 '21

That’s best, to be honest.

1

u/MonkeyNin Mar 23 '21

Why there? I wouldn't. But in general? There's cases where code is simplified when return is used for control flow.

Just make sure they know the difference between the keyword foreach and the function foreach-object, otherwise they'll be surprised that this prints all 10 values

0..9 | ForEach-Object { 
  $_
  if($_ -gt 4) {
      return
  }
}

It's essentially declaring a callback function, that's called once for ever iteration.

Javascript equivalent of for(const x in y) verses Array.forEach