Powershell – InvokeCommand versus ComputerName

As IT Pros, we will be fetching a lot of data from remote computers. But, when we are working in the production environment, we have to pay attention to the load that is exerted by the powershell cmdlets on the machine from which we run the cmdlets.

Choosing the correct cmdlet can make a huge difference in the performance.

To demonstrate this, I am browsing thru 4000 lines of System EventLogs and then selecting only those with the eventId as 1014.

I have two servers. DC and Server1. I am running the commands from Sever1. The reason why I am going thru 4000 entries of EventLog is to have a considerable amount of traffic for the Measure-Command, since the cmdlet has one local system (Server1) and one remote computer (DC).

Now, try running the below cmdlets. If you do not have a set up like this, you may use the Microsoft Virtual Labs

Cmdlet 1:

Measure-Command {Get-EventLog system -ComputerName server1, dc -Newest 4000 | where {$_.EventID -eq 1014}}

Cmdlet 2:

Measure-Command {Invoke-Command -ComputerName dc,server1 -ScriptBlock {Get-EventLog system -Newest 4000 | where {$_.EventID -eq 1014}}}

And below is the result:


If you pay attention to the number of seconds it took for each command to run, it is pretty clear that the “Invoke-Command” has a better performance compared to the cmdlet with “-ComputerName“.

This is because, by using the Invoke-Command, the processing is done locally in the remote computers. But, while using cmdlets with “-ComputerName“, all the data is first fetched to the local system (from where the cmdlet was fired) and then the processing is done locally.

We can see a difference of 4 seconds just for two computers (local and remote). But in reality we will be working with hundreds and thousands of servers, so the difference will be exponential.