# Pass varaible by reference to Invoke-Command

**URL:** <https://forums.powershell.org/t/pass-varaible-by-reference-to-invoke-command/18542>\
**Category:** PowerShell Help\
**Created:** [February 9, 2022, 10:37pm UTC](https://forums.powershell.org/t/pass-varaible-by-reference-to-invoke-command/18542 "2022-02-09T22:37:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![metablaster](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/metablaster/32/1224_2.png) [@metablaster](https://forums.powershell.org/u/metablaster)\
**Post date:** [February 9, 2022, 10:37pm UTC](https://forums.powershell.org/t/pass-varaible-by-reference-to-invoke-command/18542/1 "2022-02-09T22:37:10Z")

</div>

I need to pass local variable to remote session by reference because variable needs to be modified, for example:

```powershell
$Session = New-PSSession
$var = 0

Invoke-Command -Session $Session -ScriptBlock {
	param ([ref] $var)
	$var.Value = 5
} -ArgumentList ([ref] $var)

$var

```

I can’t use `$using:var` because that implies read-only, I get an error saying:

> InvalidData: Cannot process argument transformation on parameter ‘var’. Reference type is expected in argument.

Is there a way to send variable by reference?

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [February 10, 2022, 4:11am UTC](https://forums.powershell.org/t/pass-varaible-by-reference-to-invoke-command/18542/3 "2022-02-10T04:11:46Z")

</div>

AFAIK, I Don’t think passing variables by reference is a possible option in remote executions, variables by reference is basically pointing to the address in memory rather than passing value directly. Here both systems are not sharing the same address space.

---

<div class="post-metadata">

**Author:** ![metablaster](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/metablaster/32/1224_2.png) [@metablaster](https://forums.powershell.org/u/metablaster)\
**Post date:** [February 10, 2022, 7:17am UTC](https://forums.powershell.org/t/pass-varaible-by-reference-to-invoke-command/18542/4 "2022-02-10T07:17:36Z")

</div>

OK thank you, this makes sense.

---

<div class="post-metadata">

**Author:** ![Tent1987](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@Tent1987](https://forums.powershell.org/u/Tent1987)\
**Post date:** [February 11, 2022, 12:54pm UTC](https://forums.powershell.org/t/pass-varaible-by-reference-to-invoke-command/18542/5 "2022-02-11T12:54:27Z")

</div>

Hi,

I don’t know why u would pass it by a reference. You can pass variable the following way…  
$PassVar = Get-Process

-ScriptBlock {  
$Using:PassVar  
}

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [February 15, 2022, 1:48am UTC](https://forums.powershell.org/t/pass-varaible-by-reference-to-invoke-command/18542/6 "2022-02-15T01:48:03Z")

</div>

I do believe Matablaster’s intent was to alter the var on the remote system and have that change be reflected on the local system. Hence the “$Using:” scope would not help.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:19pm UTC](https://forums.powershell.org/t/pass-varaible-by-reference-to-invoke-command/18542/7 "2024-05-16T20:19:30Z")

</div>


