# Using sessions with Invoke-Command

**URL:** <https://forums.powershell.org/t/using-sessions-with-invoke-command/2418>\
**Category:** PowerShell Help\
**Created:** [April 3, 2014, 4:21am UTC](https://forums.powershell.org/t/using-sessions-with-invoke-command/2418 "2014-04-03T04:21:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kasun-rajapakse](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kasun-rajapakse/32/742_2.png) [@kasun-rajapakse](https://forums.powershell.org/u/kasun-rajapakse)\
**Post date:** [April 3, 2014, 4:21am UTC](https://forums.powershell.org/t/using-sessions-with-invoke-command/2418/1 "2014-04-03T04:21:38Z")

</div>

Hi all

i have come through a confusion between following two commands

**Command**

`$session = New-PSSession -ComputerName KASUNRAJAPAKSE,localhost
Get-PSSession`

`
Id Name ComputerName State ConfigurationName Availability

2 Session2 localhost Opened Microsoft.PowerShell Available
1 Session1 KASUNRAJAPAKSE Opened Microsoft.PowerShell Available
`

`Invoke-Command -Command { Get-WmiObject -Class win32_ComputerSystem } -Session ( Get-PSSession -ComputerName localhost )`

> when i run the above command it gives me the following error Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. At line:1 char:80 + Invoke-Command -Command { Get-WmiObject -Class win32\_ComputerSystem } -Session ( ... + ~ + CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand

**Command 2**

`Invoke-Command -Command { Get-WmiObject -Class win32_ComputerSystem } -Session $session[1]`

this command works as i expected

can anyone explain why first one doesn’t work

thanks

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [April 3, 2014, 4:28am UTC](https://forums.powershell.org/t/using-sessions-with-invoke-command/2418/2 "2014-04-03T04:28:48Z")

</div>

I wasn’t able to reproduce that behavior; both calls to Invoke-Command worked for me. What output do you get if you run this command?

```
Get-PSSession -ComputerName localhost
```

---

<div class="post-metadata">

**Author:** ![kasun-rajapakse](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kasun-rajapakse/32/742_2.png) [@kasun-rajapakse](https://forums.powershell.org/u/kasun-rajapakse)\
**Post date:** [April 3, 2014, 4:06pm UTC](https://forums.powershell.org/t/using-sessions-with-invoke-command/2418/3 "2014-04-03T16:06:33Z")

</div>

Hi dave

i want the same result as command 2 as shown in the image

thanks

---

<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:52pm UTC](https://forums.powershell.org/t/using-sessions-with-invoke-command/2418/4 "2024-05-16T20:52:13Z")

</div>


