# Save return.response

**URL:** <https://forums.powershell.org/t/save-return-response/19650>\
**Category:** PowerShell Help\
**Created:** [June 20, 2022, 2:19pm UTC](https://forums.powershell.org/t/save-return-response/19650 "2022-06-20T14:19:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![xhensila\_xheni](https://avatars.discourse-cdn.com/v4/letter/x/48db29/32.png) [@xhensila\_xheni](https://forums.powershell.org/u/xhensila_xheni)\
**Post date:** [June 20, 2022, 2:19pm UTC](https://forums.powershell.org/t/save-return-response/19650/1 "2022-06-20T14:19:18Z")

</div>

Hello again,

I would like to save the result that I get in this script in a variable so I can use it later

```auto
$resultclient_full_name=return $response.client_full_name
write-host "This is the result" $resultclient_full_name

```

Does it make sense?

---

<div class="post-metadata">

**Author:** ![DejvDzi](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dejvdzi/32/2460_2.png) [@DejvDzi](https://forums.powershell.org/u/DejvDzi)\
**Post date:** [June 20, 2022, 7:12pm UTC](https://forums.powershell.org/t/save-return-response/19650/2 "2022-06-20T19:12:18Z")

</div>

Hi,  
**Return** keyword is used in a function to terminate the function scope and return back to the caller scope.  
So in your case, you shouldn’t be using it

```auto
$resultclient_full_name = hostname
Write-host 'This is the result:' $resultclient_full_name

```

---

<div class="post-metadata">

**Author:** ![xhensila\_xheni](https://avatars.discourse-cdn.com/v4/letter/x/48db29/32.png) [@xhensila\_xheni](https://forums.powershell.org/u/xhensila_xheni)\
**Post date:** [June 21, 2022, 7:39am UTC](https://forums.powershell.org/t/save-return-response/19650/3 "2022-06-21T07:39:07Z")

</div>

Thank you DejvDzi for your reply. This is in the case there is only one return, what if I have it like this:

`return $response.LicenseKey,$response.Id`  
How can I save in the way that you explained before in a variable only the response for the LicenseKey?

---

<div class="post-metadata">

**Author:** ![DejvDzi](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dejvdzi/32/2460_2.png) [@DejvDzi](https://forums.powershell.org/u/DejvDzi)\
**Post date:** [June 21, 2022, 11:46am UTC](https://forums.powershell.org/t/save-return-response/19650/4 "2022-06-21T11:46:50Z")

</div>

Hi,  
Unfortunately it’s not enough information, can you show the whole function or script?

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [June 21, 2022, 1:34pm UTC](https://forums.powershell.org/t/save-return-response/19650/5 "2022-06-21T13:34:51Z")

</div>

If you have more than one value being output, you can store in more than one variable.

```auto
$first,$second = 1,2

```

If there are more than two values being output, you could add more variables to catch them else you can understand the first will get the first value and the second will get the remaining values

```auto
$first,$therest = 1,2,3,4

# contains 1
$first

#contains 2,3,4
$therest

```

---

<div class="post-metadata">

**Author:** ![xhensila\_xheni](https://avatars.discourse-cdn.com/v4/letter/x/48db29/32.png) [@xhensila\_xheni](https://forums.powershell.org/u/xhensila_xheni)\
**Post date:** [June 21, 2022, 1:34pm UTC](https://forums.powershell.org/t/save-return-response/19650/6 "2022-06-21T13:34:51Z")

</div>

Unfortunately I can not post the script here as per it has some sensible data on it but the idea is from my script to register a machine and return the Id of this machine also the license key. That is why my return is `return $response.LicenseKey,$response.Id` and I need to separate the LicenseKey and the Id from each other so when I do `invoke-expression -Command .\Get_machine_Key.ps1` in another script I can receive the LicenseKey and use it for my purposes. It would be good to save it separately here the answer in this script before I do the invoke in the other script .

Part of the script is:

```auto
$headers = @{
    "Content-Type" ="application/json"
    "X-UIPATH-TenantName" ="default"
    
    "Authorization" = " "
}

$json = $payload | ConvertTo-Json
$response = Invoke-RestMethod -Method 'Post' -Uri $registerMachineUrl -Body $json -Headers $headers

return $response.Id,$response.LicenseKey

```

Well I am not sure if my explanation makes sense but hope you can get the idea.

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [June 21, 2022, 6:03pm UTC](https://forums.powershell.org/t/save-return-response/19650/7 "2022-06-21T18:03:35Z")

</div>

PowerShell is all about the objects. Your function should return an object:

```auto
function Get-Licence {
    $response = @{
        Header = 'blah'
        User = 'bob'
        Id = 12345
        LicenceKey = 'ce4a11f0-c3c5-4f32-8bae-f101730a6d1d'
    }

    [PSCustomObject]@{
        Id = $response.Id
        LicenceKey = $response.LicenceKey 
    }
}

$data = Get-Licence

Write-Host "Id is $($data.Id), Licence Key is $($data.Licencekey)"

```

Note: you don’t even need the `return` keyword unless you specifically want to exit the function at that point.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [February 10, 2024, 5:34pm UTC](https://forums.powershell.org/t/save-return-response/19650/8 "2024-02-10T17:34:08Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
