Powershell Multithreading – Keep It Simple !

Following the #powershell hashtag on twitter, i’ve seen a los of solution to use multithreading in powershell, the used example was “launch script on many distant hots”… hmm imho fail !
Just a quick remind to the technet notes _ https://technet.microsoft.com/en-us/library/hh849719.aspx _

“When you run commands on multiple computers, Windows PowerShell connects to the computers in the order in which they appear in the list. However, the command output is displayed in the order that it is received from the remote computers, which might be different.”

Looks like there’s already MT in Invoke-Ccommand

Let’s check it

Measure-Command {501..510 |%{"Host$_"} |% { invoke-command -computername $_ -scriptblock {get-service nxlog }}}


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 8
Milliseconds      : 495
Ticks             : 84951303
TotalDays         : 9,83232673611111E-05
TotalHours        : 0,00235975841666667
TotalMinutes      : 0,141585505
TotalSeconds      : 8,4951303
TotalMilliseconds : 8495,1303
Measure-Command {invoke-command -computername ( 501..510 |%{"host$_"}) -scriptblock {get-service nxlog }}


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 1
Milliseconds      : 322
Ticks             : 13220258
TotalDays         : 1,5301224537037E-05
TotalHours        : 0,000367229388888889
TotalMinutes      : 0,0220337633333333
TotalSeconds      : 1,3220258
TotalMilliseconds : 1322,0258

So before working on overly complicated solutions based on Jobs or Threads… Read the doc 😉