| external help file | Microsoft.PowerShell.ThreadJob.dll-Help.xml |
|---|---|
| Module Name | Microsoft.PowerShell.ThreadJob |
| online version | |
| schema | 2.0.0 |
Starts a PowerShell job in a separate thread
Start-ThreadJob [-ScriptBlock] <ScriptBlock> [-Name <String>] [-InitializationScript <ScriptBlock>]
[-InputObject <PSObject>] [-ArgumentList <Object[]>] [-ThrottleLimit <Int32>] [-StreamingHost <PSHost>]
[<CommonParameters>]
Start-ThreadJob [-FilePath] <String> [-Name <String>] [-InitializationScript <ScriptBlock>]
[-InputObject <PSObject>] [-ArgumentList <Object[]>] [-ThrottleLimit <Int32>] [-StreamingHost <PSHost>]
[<CommonParameters>]
This command starts a PowerShell job running in a separate thread. It is similar to the 'Start-Job' command, except that it runs the job in a different thread instead of a child process. Consequently, it uses fewer resources for each job.
PS C:\> $job = Start-ThreadJob -ScriptBlock { 1..5 | foreach { "Output: $_"; sleep 1 }}
PS C:\> $job
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
1 Job1 ThreadJob Running False PowerShell 1..5 | foreach {
$job | Wait-Job | Receive-Job
Output: 1
Output: 2
Output: 3
Output: 4
Output: 5
PS C:\> $job
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
1 Job1 ThreadJob Completed False PowerShell 1..5 | foreach {This example starts a simple job script running in a separate thread. It then uses the 'Wait-Job' and 'Receive-Job' commands to wait for the job to complete and then return output.
PS C:\> $jobs = 1..5 | foreach { Start-ThreadJob -ScriptBlock { 1..10 | foreach { sleep 1; "Job Done" } } -ThrottleLimit 3 }
PS C:\> $jobs
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
1 Job19 ThreadJob Running True PowerShell 1..10 | foreach { sleep…
2 Job20 ThreadJob Running True PowerShell 1..10 | foreach { sleep…
3 Job21 ThreadJob Running True PowerShell 1..10 | foreach { sleep…
4 Job22 ThreadJob NotStarted False PowerShell 1..10 | foreach { sleep…
5 Job23 ThreadJob NotStarted False PowerShell 1..10 | foreach { sleep…This example runs 'Start-ThreadJob' five times to start five job instances, but with a throttle limit value of 3. Consequently, only three of the five jobs are initially running and the other two are not started but queued to run after one or more of the currently running jobs complete.
Provides an optional list of arguments to be used in the job script.
Type: Object[]
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: FalseSpecifies an optional filepath to script for the job to run.
Type: String
Parameter Sets: FilePath
Aliases:
Required: True
Position: 0
Default value: None
Accept pipeline input: False
Accept wildcard characters: FalseOptional script block for script to be run before the job script or file is run.
Type: ScriptBlock
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: FalseOptional pipeline input to be processed in the running job.
Type: PSObject
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: FalseOptional of the job created to run the script.
Type: String
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: FalseScript block that is run in the thread job.
Type: ScriptBlock
Parameter Sets: ScriptBlock
Aliases:
Required: True
Position: 0
Default value: None
Accept pipeline input: False
Accept wildcard characters: FalseOptional host object to which job output can directed.
Type: PSHost
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: FalseOptional value that determines the maximum number of thread jobs that can run at one time. If jobs are created that exceed this value, then those jobs are save in a queue until one or more running jobs complete, after which a new job will be started running.
Type: Int32
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: 5
Accept pipeline input: False
Accept wildcard characters: FalseThis cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.