Returning multiple variables from within method in powershell class


I am creating a class in powershell to manage authorization to an api. Now if for some reason the connection fails, i want to return a message and a return code.

I have a constructor, this one is calling a static method (thus i am not able to use $this...). Found some example (not used in a class though) explaining i could create a hashtable to return from my method, but for some reason this does not work.

Class ApiAuth
    [string] $APIUser
    hidden [string] $ApiPassword
    [string] $ApiUrl
    [string] $authorization
    hidden [string] $bodyParams
    [int] $ReturnCode

    ApiAuth([string] $APIUser, [string] $ApiPassword, [string] $ApiUrl)

        Write-Host "HIER1" -ForegroundColor Yellow

        $this.APIUser     = $APIUser
        $this.ApiPassword = $ApiPassword
        $this.ApiUrl      = ($ApiUrl, "Login") -join  "/"
        $this.bodyParams = @{"UserName"= $this.APIUser; "Password" = $this.ApiPassword} | ConvertTo-Json        
        $t1 = [ApiAuth]::Connect($this.ApiUrl, $this.bodyParams)

        Write-Host $t1.Length -ForegroundColor Cyan

        Write-Host $t1.ToString() -ForegroundColor RED
        Write-Host $t1['auth'] -ForegroundColor Yellow
        Write-Host $t1.code -ForegroundColor GREEN
        #Write-Host $temp[1] -ForegroundColor Yellow

        $this.authorization = [ApiAuth]::Connect($this.ApiUrl, $this.bodyParams)

    static [String] Connect([string] $ApiUrl, [string] $bodyParams)
        try {
            $rest = Invoke-WebRequest -Uri $ApiUrl -Method POST -Body $bodyParams -ContentType 'application/json'
            #$this.ReturnCode = [int]$_.Exception.Response.StatusCode
            $ret = @{}
            $ret.auth = $rest.Headers.Authorization
            $ret.code = [int]$_.Exception.Response.StatusCode

            return $ret

            #Return $rest.Headers.Authorization
            #Return $rest.Headers.Authorization, [int]$_.Exception.Response.StatusCode
        catch [System.Net.WebException] {
            Write-Host "HIER2" -ForegroundColor RED
            Write-Host $_.Exception -ForegroundColor RED
            #$this.ReturnCode = [int]$_.Exception.Response.StatusCode

            if([int]$_.Exception.Response.StatusCode -eq 401) {


            return $false
         catch {
            Write-Host "HIER3" -ForegroundColor RED
            #$this.ReturnCode = [int]$_.Exception.Response.StatusCode
            return $false



Now i am looking for a way to return multiple values to act on this accordingly. Or maybe i can call "Connect()" without making it static?


Return object

$local:o = New-Object -TypeName 'PSObject' -Property @{ 
    'ReasonText' = $reasonText;
    'ReasonCode' = $reasonCode;
return $local:o

UPDATE: Better way with PSCustomObject

return [PSCustomObject]@{ 
    'ReasonText' = $reasonText;
    'ReasonCode' = $reasonCode;

Alternative: Use [ref] inputs:

function xxx {
    Param (
    [ref]$ReasonTextOut = $Null
    if ($ReasonTextOut -eq $null) {
        $ReasonTextOut = New-Object -TypeName 'PSObject' -Propery @{Value = ''}
    $ReasonTextOut.Value = $reasonText
    return $reasonCode

$Output = '';
$code = xxx -ReasonTextOut ([ref]$Output)
Write-Host "Code: $($code); Output: $($Output)".

Personally I think that in most cases, first way is better

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at


Login to comment


Returning values from a method with multiple variables

Returning value from multiple promises within Meteor.method

Keep track of variables within class when using method multiple times

Returning Class<T> From a Method

returning multiple series from method

Returning a value from a method within a lambda expression

Method returning from one class to another class

Creating dynamic variables for a class from within the class

Calling a method on an Object from within a Class vs from within a method

returning multiple class model data from linq in list via return method in C#

Returning a value from a block in class method

The right way of returning multiple values from a method

Trouble understanding the calling of global variables with in a class to a method within the class

Variables not updated from within baseHTTPserver class

Referencing instance variables from within abstract class

Is it possible to do multiprocessing within a class method, and obtain the returning values in an order?

Using method from one class within method for other class

returning derived class from overrided method, declared as returning base copy

Calling a constructor from method within the same class

Calling super method from within super class

Determine the defining class from within a method

Instantiating multiple instances of a class from within

method in class cannot be applied - returning double value from method

Accessing class variables from ActionListener method

Returning value from callback within Meteor.method

Returning a class reference from the static method of an abstract class

Returning a reference to the derived class from a base class method

powershell output multiple variables derived from processlist

Powershell reading multiple variables from text file

TOP Ranking