Pass argument to PSSession?

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 ?

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.

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

Please do not reactivate old posts.

Please read the complete help for Invoke-Command to learn how to use local variables in script blocks. I specially recommend the example #9! :wink: