< Back

Invoke-ForEachParallel

Sat Jan 18, 2020 6:47 pm

NAME Invoke-ForEachParallel



SYNOPSIS

ForEach-Object parallel version. This is one more attempt to make parallel script execution easier.





SYNTAX

Invoke-ForEachParallel [-Process] <ScriptBlock> [-Begin <ScriptBlock>] [-End <ScriptBlock>] [-InputObject

<PSObject>] [-ThreadUnsafe <SwitchParameter>] [-ThrottleLimit <int>] [<CommonParameters>]





DESCRIPTION

This cmdlet runs script block of Process param in parallel using multiple runspaces





PARAMETERS

-InputObject <PSObject>

Specifies the input objects. The script block will be executed on each object in parallel



Required? false

Position? named

Default value

Accept pipeline input? true (ByValue)

Accept wildcard characters? false



-Process <ScriptBlock>

Specifies the operation that is performed on each input object



Required? true

Position? 0

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Begin <ScriptBlock>

Specifies a script block that runs before this cmdlet processes any input objects.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-End <ScriptBlock>

Specifies a script block that runs after this cmdlet processes all input objects.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-ThrottleLimit <int>

Sets maximum number of runspaces being created.



Required? false

Position? named

Default value 2

Accept pipeline input? false

Accept wildcard characters? false



-ThreadUnsafe <SwitchParameter>

Indicates wheither live objects or cli copies will be used



Copies are safer, but have limited functionality



Live objects have full functionality, however they should be treated with respect to concurrent access



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS

System.Management.Automation.PSObject

Specifies the input objects. The script block will be executed on each object in parallel





OUTPUTS



---------- EXAMPLE 1 ----------



Very simple example

1..5|%%{$_}

Number from 1-5 will be displayed in rundom sequence. %% is alias of this cmdlet



---------- EXAMPLE 2 ----------



Generate hash of files in c:\\temp

ls -Recurse C:\\Temp\\|%% {Get-FileHash -Path $_.FullName}

Generate files' hash in parallel. Depending on files' number and size, execution time can be bigger or less than

traditional ForEach-Object





RELATED LINKS