Find Thread Index Powershell ParallelForEach

SKG 66 Reputation points
2021-08-21T00:11:59.723+00:00

Hi All,

I am using powershell script to execute tasks in parallel. Is it possible to find the index of the thread

In following snippet how can i find which of the 5 thread is executing

ForEach -ThrottleLimit 5 -Parallel ($Procedure in $Procedure_List.Name)
{

    echo $_.ThreadIndex 

}

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. js2010 191 Reputation points
    2021-08-21T02:46:43.273+00:00
    1..5 | % -Parallel { "this is thread $_" } -ThrottleLimit 5
    
    this is thread 1
    this is thread 2
    this is thread 3
    this is thread 4
    this is thread 5
    

    Some other answers here: https://stackoverflow.com/questions/68874167/is-there-a-way-to-get-the-tid-thread-id-in-powershell-foreach-object-parallel For example managed thread id's:

    1..2 | % -Parallel { [System.Threading.Thread]::CurrentThread.ManagedThreadId }
    
    110
    100
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.