# Local variable in a session via a job

**URL:** <https://forums.powershell.org/t/local-variable-in-a-session-via-a-job/12630>\
**Category:** PowerShell Help\
**Created:** [May 28, 2019, 6:44am UTC](https://forums.powershell.org/t/local-variable-in-a-session-via-a-job/12630 "2019-05-28T06:44:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![emiltanev](https://avatars.discourse-cdn.com/v4/letter/e/5e9695/32.png) [@emiltanev](https://forums.powershell.org/u/emiltanev)\
**Post date:** [May 28, 2019, 6:44am UTC](https://forums.powershell.org/t/local-variable-in-a-session-via-a-job/12630/1 "2019-05-28T06:44:02Z")

</div>

I have a PS scheduled job that executes a script that opens a remote session. I define a variable in the script ($var), and latter I use it in the remote session. Why can’t I use this in the ps job? I can when running the script directly. The credentials for the job are the same as the logged in user.

```
Invoke-Command -Session $session -ScriptBlock {New-item "$using:var" -ItemType directory}
```

---

<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:** [May 28, 2019, 6:53am UTC](https://forums.powershell.org/t/local-variable-in-a-session-via-a-job/12630/2 "2019-05-28T06:53:38Z")

</div>

Of course you can use the variable created in a remote system using a session and access the variable in another execution using the same session. But where is the variable created, local machine or in the remote machine ?

---

<div class="post-metadata">

**Author:** ![emiltanev](https://avatars.discourse-cdn.com/v4/letter/e/5e9695/32.png) [@emiltanev](https://forums.powershell.org/u/emiltanev)\
**Post date:** [May 28, 2019, 7:02am UTC](https://forums.powershell.org/t/local-variable-in-a-session-via-a-job/12630/3 "2019-05-28T07:02:06Z")

</div>

It is the local machine. And it works when I run the script directly, rather than feed it to a ps scheduled job. So it must be something in the job.

---

<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:** [May 28, 2019, 12:21pm UTC](https://forums.powershell.org/t/local-variable-in-a-session-via-a-job/12630/4 "2019-05-28T12:21:16Z")

</div>

PS Job creates a separate PowerShell.exe for each trigger. Hence the variables won’t persisted. I would suggest you to share the code/sample here which will make this clear.

---

<div class="post-metadata">

**Author:** ![grokkit](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grokkit/32/970_2.png) [@grokkit](https://forums.powershell.org/u/grokkit)\
**Post date:** [May 28, 2019, 1:02pm UTC](https://forums.powershell.org/t/local-variable-in-a-session-via-a-job/12630/5 "2019-05-28T13:02:03Z")

</div>

Your problem is the limitation of [scopes](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-6). Specifically, “An item you include in a scope is visible in the scope in which it was created and in any child scope”.

If you run a job, and the job executes a script, and the script creates a variable, that variable is available for the script and any child processes of the script (including your remote session), but it is _not_ available to the job that executed the script because the script is a child of the job.

If you create the variable in the job, it should be available for the script and anything that the script does. Alternatively, you could try making the variable global, but this might cause problems if your script executes multiple times (resulting in each instance trying to write to the same variable). You could also explicitly make the script pass the variable up to the job.

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [May 28, 2019, 7:18pm UTC](https://forums.powershell.org/t/local-variable-in-a-session-via-a-job/12630/6 "2019-05-28T19:18:31Z")

</div>

These should help you in your use case…

About Remote Variables  
You can use variables in commands that you run on remote computers. Simply assign a value to the variable and then use the variable in place of the value.

By default, the variables in remote commands are assumed to be defined in the session in which the command runs. You can also use variables that are defined in the local session, but you must identify them as local variables in the command.

> **[about\_Remote\_Variables - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-6)**
>
> Explains how to use local and remote variables in remote commands.

Access Local Variables in a Remote Session with PowerShell

> **[Access Local Variables in a Remote Session with PowerShell](https://mikefrobbins.com/2017/11/09/access-local-variables-in-a-remote-session-with-powershell/)**
>
> Occasionally I'll come across a system with PowerShell version 2.0 on it that I need to run a remote
> command against which needs access to local variables. I …

Using a local variable in a remote PowerShell session  
[https://4sysops.com/archives/using-a-local-variable-in-a-remote-powershell-session](https://4sysops.com/archives/using-a-local-variable-in-a-remote-powershell-session)

PowerShell: Passing variables to remote commands

> **[PowerShell: Passing variables to remote commands](https://powershellexplained.com/2016-08-28-PowerShell-variables-to-remote-commands/)**
>
> If you have started using Invoke-Command, then you have ran into the issue of getting local variables into your remote commands. There is a lot of advice out there on how to do this and some approaches are more clunky than others. First let me show...

# Powershell pass variable to start-job

> <https://stackoverflow.com/questions/10075943/powershell-pass-variable-to-start-job>

```
Use the -ArgumentList parameter on Start-Job e.g.:
Start-Job -Scriptblock {param($p) "`$p is $p"} -Arg 'Server1'

# this
$pingblock = {param($servername) pathping $servername | Out-File C:\...\ServerPing.txt}
Start-Job $pingblock -Arg Server1

# or 
Start-Job -ScriptBlock {Get-ChildItem $args[0],$args[1] } -ArgumentList $a,$b
```

How to pass variables to Start-Job?

> <https://stackoverflow.com/questions/41764926/how-to-pass-variables-to-start-job>

This is how you can pass a value inside Start-Job.

You have to invoke it from inside. This is my blog link for the same: Start-Job Passing value

```
$ini='$var="'+$args[0]+'"'
$a={
 Function Get-add()
        {
        "this is the value of $var"

        }
 }
start-job -InitializationScript $a -ScriptBlock {param($ini)iex $ini;get-add } -ArgumentList $ini |Wait-Job | Receive-Job
```

See also:

About Remote Variables  
You can use variables in commands that you run on remote computers. Simply assign a value to the variable and then use the variable in place of the value.

By default, the variables in remote commands are assumed to be defined in the session in which the command runs. You can also use variables that are defined in the local session, but you must identify them as local variables in the command.

> **[about\_Remote\_Variables - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-6)**
>
> Explains how to use local and remote variables in remote commands.

Access Local Variables in a Remote Session with PowerShell

> **[Access Local Variables in a Remote Session with PowerShell](https://mikefrobbins.com/2017/11/09/access-local-variables-in-a-remote-session-with-powershell/)**
>
> Occasionally I'll come across a system with PowerShell version 2.0 on it that I need to run a remote
> command against which needs access to local variables. I …

Using a local variable in a remote PowerShell session  
[https://4sysops.com/archives/using-a-local-variable-in-a-remote-powershell-session](https://4sysops.com/archives/using-a-local-variable-in-a-remote-powershell-session)

PowerShell: Passing variables to remote commands

> **[PowerShell: Passing variables to remote commands](https://powershellexplained.com/2016-08-28-PowerShell-variables-to-remote-commands/)**
>
> If you have started using Invoke-Command, then you have ran into the issue of getting local variables into your remote commands. There is a lot of advice out there on how to do this and some approaches are more clunky than others. First let me show...

---

<div class="post-metadata">

**Author:** ![emiltanev](https://avatars.discourse-cdn.com/v4/letter/e/5e9695/32.png) [@emiltanev](https://forums.powershell.org/u/emiltanev)\
**Post date:** [May 30, 2019, 9:41am UTC](https://forums.powershell.org/t/local-variable-in-a-session-via-a-job/12630/7 "2019-05-30T09:41:13Z")

</div>

Ended up using the script params and -argumentlist as below. Thanks.

```
Invoke-Command -Session $session -ScriptBlock {param($var) New-item $var -ItemType directory} -ArgumentList $var
```

---

<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:32pm UTC](https://forums.powershell.org/t/local-variable-in-a-session-via-a-job/12630/8 "2024-05-16T20:32:43Z")

</div>

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


