# Pass argument to PSSession?

**URL:** <https://forums.powershell.org/t/pass-argument-to-pssession/2185>\
**Category:** PowerShell Help\
**Created:** [January 7, 2014, 5:42am UTC](https://forums.powershell.org/t/pass-argument-to-pssession/2185 "2014-01-07T05:42:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kitt-holland](https://avatars.discourse-cdn.com/v4/letter/k/e99b99/32.png) [@kitt-holland](https://forums.powershell.org/u/kitt-holland)\
**Post date:** [January 7, 2014, 5:42am UTC](https://forums.powershell.org/t/pass-argument-to-pssession/2185/1 "2014-01-07T05:42:04Z")

</div>

I know I can use argumentlists, or $using for Invoke-Command for executing scriptblocks remotely, however I was wondering if there was any way to pass an argument into an interactive pssession with Enter-PSSession or New-PSSession ?

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [January 8, 2014, 2:15am UTC](https://forums.powershell.org/t/pass-argument-to-pssession/2185/2 "2014-01-08T02:15:47Z")

</div>

Well, those are two different questions.

You can’t “pass arguments into a session.” A session is a runspace on a machine; what you can certainly do is run a command in that runspace. A session isn’t running anything implicitly, so there’s no need for arguments.

```
$s = New-PSSession -computername SERVER
Invoke-Command -Session $s -scriptblock { $var = 5 }
```

The remote session now has $var equal to 5.

WRT Enter-PSSession, it’s the same story. You’re just interacting with a runspace. If you want to create a variable in that runspace, just do so. But the command doesn’t offer a way to pre-pass anything into the runspace, no. That isn’t really what sessions are for.

Invoke-Command is different because it’s actually running a command, so there’s an obvious need for arguments.

---

<div class="post-metadata">

**Author:** ![psiscrazy](https://avatars.discourse-cdn.com/v4/letter/p/34f0e0/32.png) [@psiscrazy](https://forums.powershell.org/u/psiscrazy)\
**Post date:** [February 6, 2018, 2:51pm UTC](https://forums.powershell.org/t/pass-argument-to-pssession/2185/3 "2018-02-06T14:51:07Z")

</div>

Rather than explicitly setting $var to 5 how do you set $var to a variable that was set before executing the New-PSSession?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 6, 2018, 3:03pm UTC](https://forums.powershell.org/t/pass-argument-to-pssession/2185/4 "2018-02-06T15:03:02Z")

</div>

Please do not reactivate old posts.

Please read the complete help for [Invoke-Command](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-5.1) to learn how to use local variables in script blocks. I specially recommend the example #9! 😉

---

<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:36pm UTC](https://forums.powershell.org/t/pass-argument-to-pssession/2185/5 "2024-05-16T20:36:26Z")

</div>

Closed on May 16, 2024, 8:36pm UTC


