Sometimes, running remote code with the PowerShell cmdlet Invoke-Command can take a long time to run—and not for any reason having to do with your code itself. Connecting to older computers means that long bits of code take longer to run. Computers on the other side of slow WAN links will always be slower in responding than something close and well connected.

So, what can you do to help in those situations? Run PowerShell code as jobs.

There isn’t much you can do to speed up a slow computer. You can optimize your code to make it run faster once it gets there, but at some point, the end node’s ability to process commands will limit you. But that doesn’t mean you need to wait on your code to complete.

One easy option is to submit your Invoke-Command as a job. That will tell the computer to run it in the background and give you back the console so you can work on other tasks. Following up from the example in the previous article in this series, those sessions are still available to use.

Here’s an example of code that I would rather not wait to finish. I will ping Google from all nine servers.

Invoke-Command -Session $sessions -ScriptBlock {ping}
Pinging multiple servers

Pinging multiple servers

I’ve chopped off the bulk of the output because it just runs on for some time. This isn’t elegant code, but it illustrates a point. If I had to run this code, I wouldn’t want to wait for responses from many computers. Instead, I can run the same code with one extra parameter and be free to move on to other tasks. This tells PowerShell to run the same command but run it in the background.

Invoke-Command -Session $sessions -ScriptBlock {ping}  -AsJob
Invoke Command as job

Invoke Command as job

Now, I get a job summary instead of output. I run the Get-Job cmdlet to see the status of the current job. Notice it says RemoteJob.

Job summary

Job summary

I see that RemoteJob finished. To get the results, I run the cmdlet Receive-Job to transfer the job results from the queue to my console. I can add the -keep parameter to save the results for future review. If I don’t add -Keep, the results are deleted from the job queue and only exist in my console session. They cannot be recalled again.

receive-job 23 -keep
Receiving jobs

Receiving jobs

The results show that it displays the same output as when we ran the command in the console prompt earlier. Except this time, I didn’t need to wait for all the pings to finish; the results display instantly.

Subscribe to 4sysops newsletter!


In this series of three parts, we have reviewed how we can deal with situations that can cause problems when connecting to remote computers using Invoke-Command. These three tips only scratch the surface of what you can do to optimize code to help you get things done as efficiently as possible. Look for more tips on remoting and executing code in bulk here at 4sysops. I would love to know your thoughts. Please use the comments section below to reach out with your questions and ideas.

Articles in series

Invoke-Command: Common challenges

1 Comment
  1. Leos Marek 3 years ago

    Interesting series here. In the end, if we speak about the speed of execution, it should not matter if you run in at job or interactively, the time needed is still the same. The only difference is that you can do some other things while waiting 🙂


Leave a reply

Your email address will not be published.


© 4sysops 2006 - 2022


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account