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:

1function New-MrGuid {
2    $Guid = [System.Guid]::NewGuid()
3    Return $Guid
4}

return1a.jpg

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:

1function New-MrGuid {
2    $Guid = [System.Guid]::NewGuid()
3    $Guid
4}

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:

1function New-MrGuid {
2    $Guid = [System.Guid]::NewGuid()
3    Write-Output $Guid
4}

return1a.jpg

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:

1function New-MrGuid {
2    [System.Guid]::NewGuid()
3}

return1a.jpg

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

 1function Test-Return {
 2    [CmdletBinding()]
 3    param (
 4        [int[]]$Number
 5    )
 6
 7    foreach ($N in $Number) {
 8        if ($N -ge 4) {
 9            $N
10        }
11    }
12}

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

1Test-Return -Number 3, 5, 7, 9

return2a.jpg

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

 1function Test-Return {
 2    [CmdletBinding()]
 3    param (
 4        [int[]]$Number
 5    )
 6
 7    foreach ($N in $Number) {
 8        if ($N -ge 4) {
 9            Return $N
10        }
11    }
12}

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.

1Test-Return -Number 3, 5, 7, 9

return3a.jpg

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:

 1function Test-Return {
 2    [CmdletBinding()]
 3    param (
 4        [int[]]$Number
 5    )
 6
 7    $i = 0
 8
 9    foreach ($N in $Number) {
10        if ($N -ge 4) {
11            $i++
12            $N
13        }
14    }
15
16    Write-Verbose -Message "A total of $i items were returned."
17}

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

return4a.jpg

The return keyword has been added to the following function:

 1function Test-Return {
 2    [CmdletBinding()]
 3    param (
 4        [int[]]$Number
 5    )
 6
 7    $i = 0
 8
 9    foreach ($N in $Number) {
10        if ($N -ge 4) {
11            $i++
12            Return $N
13        }
14    }
15
16    Write-Verbose -Message "A total of $i items were returned."
17}

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:

return5a.jpg

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:

return6a.jpg

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.

µ