# Invoke-Command with multiple parameters

**URL:** <https://forums.powershell.org/t/invoke-command-with-multiple-parameters/4467>\
**Category:** PowerShell Help\
**Created:** [July 3, 2015, 4:57pm UTC](https://forums.powershell.org/t/invoke-command-with-multiple-parameters/4467 "2015-07-03T16:57:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarah-urmeneta](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@sarah-urmeneta](https://forums.powershell.org/u/sarah-urmeneta)\
**Post date:** [July 3, 2015, 4:57pm UTC](https://forums.powershell.org/t/invoke-command-with-multiple-parameters/4467/1 "2015-07-03T16:57:34Z")

</div>

This command works for me

Invoke-Command {param($Param1) C:\test.ps1 -Param1 $Param1 } -ArgumentList $MyParam1

When I add a new integer parameter to my test.ps1 file, I cannot get it to work. I tried this

Invoke-Command {param($Param1, $intParam) C:\test.ps1 -Param1 $Param1 -intParam $intParam } -ArgumentList $MyParam1, 1

What is the correct syntax for this?

---

<div class="post-metadata">

**Author:** ![openiduser3](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/openiduser3/32/421_2.png) [@openiduser3](https://forums.powershell.org/u/openiduser3)\
**Post date:** [July 3, 2015, 5:10pm UTC](https://forums.powershell.org/t/invoke-command-with-multiple-parameters/4467/2 "2015-07-03T17:10:13Z")

</div>

Looks right, according to the help for the -ArgumentList parameter.

Starting with Powershell version 3, you can also pass local variables by prefacing them with using: . See Example 9.

Invoke-Command  
[https://technet.microsoft.com/library/906b4b41-7da8-4330-9363-e7164e5e6970(v=wps.630).aspx](https://technet.microsoft.com/library/906b4b41-7da8-4330-9363-e7164e5e6970%28v=wps.630%29.aspx)

---

<div class="post-metadata">

**Author:** ![sarah-urmeneta](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@sarah-urmeneta](https://forums.powershell.org/u/sarah-urmeneta)\
**Post date:** [July 4, 2015, 3:05pm UTC](https://forums.powershell.org/t/invoke-command-with-multiple-parameters/4467/3 "2015-07-04T15:05:24Z")

</div>

It does not throw an error but it’s not passing the integer parameter. I verified by writing to a file both parameters.

---

<div class="post-metadata">

**Author:** ![flynnbundy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/flynnbundy/32/639_2.png) [@flynnbundy](https://forums.powershell.org/u/flynnbundy)\
**Post date:** [July 4, 2015, 5:36pm UTC](https://forums.powershell.org/t/invoke-command-with-multiple-parameters/4467/4 "2015-07-04T17:36:25Z")

</div>

As Nathan said. You should just be able to do this:

```
$x = 'Hello'
Invoke-Command -Computername Blah { Write-Host $using:x }
```

after PowerShell 3.0 its very simple with $using:. You may need to link some of the code you are working with if you are still having trouble.

---

<div class="post-metadata">

**Author:** ![\_timpringle](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/_timpringle/32/276_2.png) [@\_timpringle](https://forums.powershell.org/u/_timpringle)\
**Post date:** [July 4, 2015, 6:06pm UTC](https://forums.powershell.org/t/invoke-command-with-multiple-parameters/4467/5 "2015-07-04T18:06:51Z")

</div>

Hi Kahlan,

Can you try this and let me know if it works? Check the text file created.

```
#requires -Version 2
$argList = @{
    Param1 = 'whatever that is'
    intParam = 1
}

Invoke-Command -ComputerName myserver -ScriptBlock {
    param($argList) 
   $argList | out-file -Path c:\windows\temp\myvariables.txt
    C:\test.ps1 -Param1 $argList.Param1 -intParam $argList.intParam
} -ArgumentList $argList
```

---

<div class="post-metadata">

**Author:** ![sarah-urmeneta](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@sarah-urmeneta](https://forums.powershell.org/u/sarah-urmeneta)\
**Post date:** [July 5, 2015, 10:31am UTC](https://forums.powershell.org/t/invoke-command-with-multiple-parameters/4467/6 "2015-07-05T10:31:44Z")

</div>

It’s still not working as expected. The content of the myvariables.txt is fine.  
Name Value

* * *

ProfileTokens whatever that is  
TaskID 1

But in the PowerShell script file I’m trying to execute, the integer parameter is treated as part of the first parameter as I observed when I printed the values of both parameters. By the way, this is the content of the PS file I’m trying to execute:

param($ProfileTokens, $TaskID)

Function Main ($ProfileTokens, $TaskID)  
{  
–printing the values of the parameters  
}

Main $ProfileTokens, $TaskID

---

<div class="post-metadata">

**Author:** ![sarah-urmeneta](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@sarah-urmeneta](https://forums.powershell.org/u/sarah-urmeneta)\
**Post date:** [July 5, 2015, 10:39am UTC](https://forums.powershell.org/t/invoke-command-with-multiple-parameters/4467/7 "2015-07-05T10:39:15Z")

</div>

I found the culprit, it’s the comma inside my PS file which calls the main function. Whew! Thanks for your help!

---

<div class="post-metadata">

**Author:** ![sarah-urmeneta](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@sarah-urmeneta](https://forums.powershell.org/u/sarah-urmeneta)\
**Post date:** [July 5, 2015, 10:46am UTC](https://forums.powershell.org/t/invoke-command-with-multiple-parameters/4467/8 "2015-07-05T10:46:07Z")

</div>

I do have a follow up though on this. How can I specify a variable inside a script block? it seems like it does not properly expand the variables. If I specify

$Tokens = @{}  
#Tokens is a dictionary, add values here

$argList = @{  
ProfileTokens = $Tokens  
TaskID = 1  
}  
Invoke-Command -ScriptBlock {param($argList)  
C:\Test.ps1 -ProfileTokens $argList.ProfileTokens -TaskID $argList.TaskID  
} -ArgumentList $argList

This results to $argList.ProfileTokens to be passed to the PS file as null.

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [July 5, 2015, 11:45am UTC](https://forums.powershell.org/t/invoke-command-with-multiple-parameters/4467/9 "2015-07-05T11:45:15Z")

</div>

Just do a simple test:

```
PS C:\Users\Rob> $test = {}

PS C:\Users\Rob> $test.GetType()

IsPublic IsSerial Name BaseType                                                                                                                                      
-------- -------- ---- --------                                                                                                                                      
True True ScriptBlock System.Object
```

So, you’re basically making the Invoke-Command a scriptblock. Try specifying the arguments in the scriptblock with using:

---

<div class="post-metadata">

**Author:** ![sarah-urmeneta](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@sarah-urmeneta](https://forums.powershell.org/u/sarah-urmeneta)\
**Post date:** [July 5, 2015, 1:11pm UTC](https://forums.powershell.org/t/invoke-command-with-multiple-parameters/4467/10 "2015-07-05T13:11:27Z")

</div>

I’m not sure I understand. Could you give the sample script?  
I also don’t see $using: in the intellisense and it results to an error if I use it. I think I read somewhere that it only works if executed in a remote machine. I’m doing this and invoking the command on a local machine

---

<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:45pm UTC](https://forums.powershell.org/t/invoke-command-with-multiple-parameters/4467/11 "2024-05-16T20:45:19Z")

</div>


