PowerShell for Windows Admins

Jul 24 2018   5:38AM GMT

PowerShell ThreadJobs

Richard Siddaway Richard Siddaway Profile: Richard Siddaway


PowerShell Jobs were introduced in PowerShell v2. While remoting got the attention in that release PowerShell Jobs are at least as important. PowerShell v6.1 preview 4 has introduced a new addition to the options for jobs – PowerShell ThreadJobs.

The real change is the addition of the Start-ThreadJob cmdlet. All other aspects of managing ThreadJobs are provided by the standard Jobs cmdlets.

PS>  Get-Command *Job | select name


When you use Start-Job the job is run in the background on a SEPARATE PROCESS. You can see the PowerShell processes stopping and starting if you monitor via Task manager or another instance of PowerShell.

ThreadJobs run on a separate THREAD within the same process. Apart for the cmdlet name you start a threadjob in the same way as a traditional background job.

PS>  Start-Job -ScriptBlock {Get-Service}

Id   Name   PSJobTypeName   State    HasMoreData  Location  Command
--   ----   -------------   -----    -----------  --------  -------
1    Job1   BackgroundJob   Running  True         localhost Get-Service

PS>  Start-ThreadJob -ScriptBlock {get-Service}

Id   Name  PSJobTypeName   State       HasMoreData  Location    Command
--   ----  -------------   -----       -----------  --------    -------
3    Job3  ThreadJob       NotStarted  False        PowerShell  get-Service

Notice the different job type name. The location changes from the localhost to PowerShell indicating its not a separate process.

To view jobs

PS>  Get-Job

Id     Name   PSJobTypeName   State       HasMoreData   Location     Command
--     ----   -------------   -----       -----------   --------     -------
1      Job1   BackgroundJob   Completed   True          localhost    Get-Service
3      Job3   ThreadJob       Completed   True          PowerShell   get-Service

To view results

PS>  Receive-Job -Id 3

Status   Name               DisplayName
------   ----               -----------
Running  AdobeARMservice    Adobe Acrobat Update Service
Stopped  AJRouter           AllJoyn Router Service
Stopped  ALG                Application Layer Gateway Service
Stopped  AppIDSvc           Application Identity

To clean up jobs

PS>  Get-Job | Remove-Job
PS>  Get-Job

The advantage of ThreadJobs is that they are lighter on resource consumption because they are using a thread not a process. You can therefore run more ThreadJobs simultaneously compared to standard Jobs.

The disadvantage for ThreadJobs is that all of your jobs are running in the same process. If one crashes the process will most likely crash and you’ll lose everything.

The pros and cons of using ThreadJobs vs standard jobs must be addressed on a case by case basis. The ThreadJobs project has a bit more background – https://github.com/PaulHigin/PSThreadJob

ThreadJobs look to be a useful addition to the options for running background jobs.

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: