# Invoke-Command + Get-ChildItem = Output?

**URL:** <https://forums.powershell.org/t/invoke-command-get-childitem-output/14924>\
**Category:** PowerShell Help\
**Created:** [September 10, 2020, 5:49am UTC](https://forums.powershell.org/t/invoke-command-get-childitem-output/14924 "2020-09-10T05:49:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![izshell](https://avatars.discourse-cdn.com/v4/letter/i/3bc359/32.png) [@izshell](https://forums.powershell.org/u/izshell)\
**Post date:** [September 10, 2020, 5:49am UTC](https://forums.powershell.org/t/invoke-command-get-childitem-output/14924/1 "2020-09-10T05:49:11Z")

</div>

Hello!

I have a script that used Start-Job to start Get-ChildItem script. For reasons that are too long to get into, I wanted to try this methodology with Invoke-Command instead!

The command looks like this:

```
$a = Invoke-Command -AsJob -ComputerName $env:computername -ScriptBlock {
Get-ChildItem -Path C:\ *.exe }
echo $a
```

However, instead of $a containing the output for the Get-Childitem command ( a list of executables), it instead contains the output for the job!

```
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
15 Job15 RemoteJob Running True WIN-GNNG6UDHR6J ...
```

What am I doing wrong?  
I have attempted to use Receive-Job but have had no success yet. This is the error I get when I try to receive-job

```
Connecting to remote server WIN-GNNG6UDHR6J failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (WIN-GNNG6UDHR6J:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : AccessDenied,PSSessionStateBroken
```

Thanks ahead of time!

---

<div class="post-metadata">

**Author:** ![adminofthings](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@adminofthings](https://forums.powershell.org/u/adminofthings)\
**Post date:** [September 10, 2020, 8:40am UTC](https://forums.powershell.org/t/invoke-command-get-childitem-output/14924/2 "2020-09-10T08:40:22Z")

</div>

If you want the output of the remote command to be stored in `$a`, don’t use the `-AsJob` switch. If you continue to use a job, then once `$a` shows a `State` other than `Running`, you can use `$a | Receive-Job` for the output. Access denied errors could be a number of things. I’d make sure the account running `Invoke-Command` has access to perform the remote command as if it were running the command locally on the target system. Admin elevation may be required in the shell as well.

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [September 11, 2020, 4:57am UTC](https://forums.powershell.org/t/invoke-command-get-childitem-output/14924/3 "2020-09-11T04:57:23Z")

</div>

Here’s an example. In powershell 7 it comes out blank though, lol. For this example, the prompt has to be elevated.

```
invoke-command localhost -asjob { dir } | receive-job -wait -AutoRemoveJob
```

---

<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:28pm UTC](https://forums.powershell.org/t/invoke-command-get-childitem-output/14924/4 "2024-05-16T20:28:54Z")

</div>

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


