Search code examples
powershellunzip

Unzip multiple files in parallel using powershell


I am trying to achieve parallel unzip using PowerShell. The code attached would unzip one file at a time that matches the keyword. Is there a way I could unzip multiple files in parallel

Code


Solution

  • You could use a workflow which will provide parallel processing You can learn alot more about workflows at https://learn.microsoft.com/en-us/system-center/sma/overview-powershell-workflows?view=sc-sma-1801

    workflow Unzip-File{
        Param (
            [Object]$Files,
            [string]$Destination,
            [switch]$SeprateFolders
        )
        foreach –parallel ($File in $Files){
            if($SeprateFolders){
                Write-Output "$($file.Name) : Started"
                Expand-Archive -Path $File -DestinationPath "$Destination\$($file.Name)"
                Write-Output "$($file.Name) : Completed"
            }else{
                Write-Output "$($file.Name) : Started"
                Expand-Archive -Path $File -DestinationPath $Destination
                Write-Output "$($file.Name) : Completed"
            }      
        }
    }
    
    try{
        $ZipFiles = Get-ChildItem C:\Users\Default\Desktop\ZipTest\Source\*.zip
        Unzip-File -Files $ZipFiles -Destination "C:\Users\Default\Desktop\ZipTest\Destination" -SeprateFolders
    }catch{
        Write-Error $_
    }