Again I challenged myself to make a PowerShell script, this time I wanted to stress test a file server so I needed to start multiple file copies from one source folder to multiple destination folder on a share. To somehow simulate multiple users copying files I would also need to start the script from more than one computer.

The script will start background jobs that will copy all files and folders in a specific folder and you can configure how many jobs to run in parallel and how long it should sleep until checking to start the next job. The DestPaths variable is an array so you can add how many folder you would like or if you’d like to copy files to different computer.

An other good example would be when I run a workshop and need to copy files to all computers and don’t want to overload the server or the network this script will for certain help me with that!

Here is the script, you are more than happy to edit and build the script in your way, but it would be nice if you can share your script with us!

$SourcePath = “c:\Temp\1\*”
$DestPaths = “c:\Temp\2\”, “C:\Temp\3\”, “C:\Temp\4\”, “C:\Temp\5\”, “C:\Temp\6\”, “c:\Temp\7\”, “C:\Temp\8\”, “C:\Temp\9\”, “C:\Temp\10\”, “C:\Temp\11\”
$ParallelJobs = 5
$JobCheckSleepTime = 10

Foreach ($DestPath in $DestPaths)
    $Path = Get-Item -Path $DestPath -Force -ErrorAction SilentlyContinue

    while (($JobsRunning = ((Get-Job -State Running).Count)) -ge $ParallelJobs)
        Write-Host “Sleeping for $JobCheckSleepTime seconds. There are $JobsRunning jobs already running…” -ForegroundColor Yellow
        #Enable for debug reasons
        #Get-Job -State Running | Format-Table

        Start-Sleep -S $JobCheckSleepTime       

    #Create destination folder if not already exists
    if ($Path.Exists -ne $true)
        Write-Host “Create directory $DestPath”
        New-Item -Path $DestPath -ItemType directory -Force -ErrorAction SilentlyContinue | Out-Null

    Write-Host “Starting job ” -NoNewline
    $SBCopyItem = [scriptblock]::Create(“Copy-Item -Path $SourcePath -Destination $DestPath  -Recurse -Force -ErrorAction SilentlyContinue”)
    #Start-Job -ScriptBlock $SBCopyItem | Out-Null
    $JobId = (Start-Job -ScriptBlock $SBCopyItem).Id
    Write-Host “($JobId) to copy files to $DestPath”

if ((Get-Job -State Running).Count -ne 0)
    Write-Host “Note there are jobs still running!”
    Get-Job -State Running