Parallel copy files over remoting session

metro-powershell-logoA fairly straightforward method to copy files via PowerShell remote sessions to multiple computers in parallel.

$ErrorActionPreference = 'stop'

workflow Copy-Parallel {
    param ([string[]]$ComputerName, [string]$SourcePath,[string]$TargetPath)
    foreach -parallel ($computer in $ComputerName) {
        InlineScript  {
            try {
                $s = New-PSSession -ComputerName $using:computer
                Copy-Item -Path $using:SourcePath -Destination (Split-Path $using:TargetPath -Parent) -ToSession $s
            } finally {
                Remove-PSSession -Session $s

function New-BigFile {
        [int]$SizeInBytes = 30MB
    $bytes = new-object byte[] 1000000
    try {
        $fs = [io.file]::Create($filePath)
        $bytesWritten = 0
        do {
            $fs.Write($bytes, 0, $bytes.Count)
            $bytesWritten += $bytes.Count
        } while($bytesWritten -lt $SizeInBytes)
    } finally {

$filePath = 'c:\temp\bigfile.bin'
New-BigFile -Path $filePath -SizeInBytes 100MB
Copy-Parallel -ComputerName dev-bldserv2,dev-bldagnt4,dev-bldagnt5,devbuild8 -SourcePath $filePath -TargetPath $filePath

Richard Siddaway's Blog

In response to this post –

I was asked how you could copy files in parallel to multiple machines.

As soon as anyone mentions parallelism I think of workflows so I ended up with this

$computers = ‘W16TP5TGT01’, ‘W16TP5TGT02’

workflow parallelcopy {
param ([string[]]$computername)
foreach -parallel ($computer in $computername) {
InlineScript {
$s = New-PSSession -ComputerName $using:computer
Copy-Item -Path C:ScriptsNew-NanoMachine.ps1 -Destination C:Source -ToSession $s
Remove-PSSession -Session $s

parallelcopy -computername $computers

A couple of issues I found. First off –Tosession and –FromSession haven’t been added to the Copy-Item workflow activity. This means you have to use an Inline script block to access those parameters

Secondly accessing a emoting session created outside of the workflow generates a session busy error when trying to perform the copy so have to move the session creation into the Inline script.

If you had a lot of files, or a lot of…

View original post 26 more words