PowerShell Genie

Start-ThreadJob

Use Start-ThreadJob to run a script block on a thread inside the current process and return a job object.

Command 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — PowerShell module reference

Syntax

1PS> Start-ThreadJob [-ScriptBlock] <ScriptBlock> [-Name <String>] [-ThrottleLimit <Int32>] [-ArgumentList <Object[]>] [-InitializationScript <ScriptBlock>] [-StreamingHost <PSHost>] [<CommonParameters>]
See the command anatomy

Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

Start-ThreadJob [-ScriptBlock] <ScriptBlock> [-Name <String>] [-ThrottleLimit <Int32>] [-ArgumentList <Object[]>] [-InitializationScript <ScriptBlock>] [-StreamingHost <PSHost>] [<CommonParameters>]
  1. Stage 1Start-ThreadJob-ScriptBlock -Name -ThrottleLimit -ArgumentList -InitializationScript -StreamingHost

    Try it

    Windows PowerShell

    PowerShell example · browser support is limited.

    Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.

    Interactive simulation · fictional data

    Examples

    Start-ThreadJob -Name quick

    1PS> Start-ThreadJob -Name quick -ScriptBlock { Get-Service | Measure-Object | Select-Object Count } | Select-Object Id, Name, State
    Show reference output

    Sample output · fictional data, shaped like the real thing

    Id  Name   State
    --  ----   -----
    1   quick  Running

    Start-ThreadJob -ScriptBlock {

    1PS> Start-ThreadJob -ScriptBlock { param($n) $n * 2 } -ArgumentList 21 -ThrottleLimit 10 | Select-Object Id, State
    Show reference output

    Sample output · fictional data, shaped like the real thing

    Id  Name  State    HasMoreData  Location
    --  ----  -----    -----------  --------
    1   Job1  Running  True         localhost

    Select Name, Version

    1PS> Get-Module ThreadJob -ListAvailable | Select-Object Name, Version
    Show reference output

    Sample output · fictional data, shaped like the real thing

    Name              Version
    ----              -------
    ThreadJob         2.0.3

    Parameters

    Parameter definitions:

    • -ScriptBlock
    • -Name
    • -ThrottleLimit
    • -ArgumentList
    • -InitializationScript
    • -StreamingHost

    PowerShell module reference on Microsoft Learn

    Practical use and troubleshooting

    Practical scenario: Use Start-ThreadJob when you need to run a script block on a thread inside the current process and return a job object. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.

    Common error and fix: Command not recognised can mean ThreadJob is unavailable in that environment. Check Get-Module -ListAvailable ThreadJob and install the module only through the approved repository when needed. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.

    See also: Start-Job. Start-Job uses a separate process; Start-ThreadJob uses threads in the current process. Process jobs serialize results, while thread jobs share a process and its failure boundary. Read the linked command before substituting it; similar names do not imply identical inputs or output.

    Start-ThreadJob reference on Microsoft Learn

    Recap

    Start-ThreadJob Use Start-ThreadJob to run a script block on a thread inside the current process and return a job object.

    Next: Start-Job
    Genie guideStart-ThreadJob

    Use Start-ThreadJob to run a script block on a thread inside the current process and return a job object. The console below is pre-loaded — run it, then change a value and run it again.