The PowerShell return keyword

The return keyword is probably the most over used keyword in PowerShell that’s used in unnecessary scenarios. You’ll often find it used to simply return the output of a function:


In that scenario, using the return keyword is totally unnecessary. If you do want to return the value of the variable, simply let PowerShell take care of returning the output:

I received a comment on Twitter from Bill Hurt about using the Write-Output cmdlet in this scenario and although I didn’t specify it in the previous example, I typically do use Write-Output instead of just specifying the variable itself:


In the previous example, there’s no reason to store the value in a variable, simply create the new GUID and let PowerShell handle returning the output all in one command:


The return keyword does have a valid use case though. The following function does not use the return keyword:

Without the return keyword any number greater than or equal to four is returned:


Notice that the return keyword has been added to the function without any other changes:

With the return keyword, the first value that is greater than or equal to 4 will be returned and then the foreach loop will exit without testing the numbers 7 or 9.


Not only does it exit the foreach loop, but the entire function so if additional code existed after the foreach loop, it wouldn’t be executed either. A slightly modified version of the previous function will be used to demonstrate this.

For the first test, the return keyword is omitted:

The verbose output after the foreach loop is included in the output when specifying the verbose parameter:


The return keyword has been added to the following function:

Notice that although the verbose parameter was specified, the verbose output is not included because the return keyword causes the function to exit before it gets to that point:


Of course, if the portion of the code with the return keyword isn’t run, the verbose output will be included in the output when the verbose parameter is specified:


As Jaap Brasser noted via a comment to this post, it’s worth noting that the return keyword is actually required when working with classes in PowerShell version 5 so that’s another valid use case for it. I’ll plan to cover classes in a future blog article.

For more information about the return keyword, see the about_Return help topic.



  1. Jaap Brasser

    It might be worth noting that the return keyword is actually required when working with classes in PowerShell 5.0. That is the only where you actually cannot avoid working with the return statement.

  2. Craig

    This post solved an issue I had been battling to understand with a script I was using to generate complex passwords, thanks for sharing

  3. MMorrison

    Great article. I was initially wondering why you couldn’t just specify the variable (which would handle outputting it) and then using Break below it to exit the loop, but in that scenario any further code would still run. The return in this example has a unique use case.

    It would in that sense still share similarities to Exit but then that would close your PowerShell host, which might not be ideal. Thank you for sharing the info in a clear manner.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: