How to return one and only one value from a PowerShell function?

前端 未结 10 904
暖寄归人
暖寄归人 2020-12-24 01:17

I\'ve learned from this Stack Overflow question, that PowerShell return semantics are different, let\'s say, from C#\'s return semantics. Quote from the aforementioned quest

10条回答
  •  孤独总比滥情好
    2020-12-24 01:53

    You can also just assign everything to null, and then return just the variable you want:

    Function TestReturn {
    
        $Null = @(
            Write-Output "Hi there!"
            1 + 1
            $host
            $ReturnVar = 5
        )
        return $ReturnVar
    }
    

提交回复
热议问题