Public/Expand-specZipFile.ps1
function Expand-specZipFile { <# .SYNOPSIS Extracts the contents of specified zip files to a destination folder. .DESCRIPTION The Expand-specZipFile function extracts the contents of one or more specified zip files to a specified destination folder. It utilises the Expand-Archive cmdlet to perform the extraction. If any errors occur during the extraction process, they are caught and output as a PSCustomObject and a warning is displayed. .PARAMETER Path Specifies the path(s) to the zip file(s) that you want to extract. You can specify multiple paths by providing an array of strings. .PARAMETER Destination Specifies the destination folder where the contents of the zip files will be extracted. .EXAMPLE Expand-specZipFile -Path 'C:\Temp\archive1.zip','C:\Temp\archive2.zip' -Destination 'C:\ExtractedFiles' Extracts the contents of 'archive1.zip' and 'archive2.zip' to the 'C:\ExtractedFiles' folder. .EXAMPLE $x = Expand-specZipFile -Path 'C:\Temp\archive1.zip','C:\Temp\archive2.zip' -Destination 'C:\ExtractedFiles' if ($x.count -gt 0) { Write-Host "An error occurred" } Extracts the contents of 'archive1.zip' and 'archive2.zip' to the 'C:\ExtractedFiles' folder. If any errors occur during the extraction process, a message indicating that an error occurred is displayed. $x is an object and you can query its properties for specifics, such as the error message, and take appropriate action. .EXAMPLE 'C:\Temp\archive1.zip','C:\Temp\archive2.zip' | Expand-specZipFile -Destination 'C:\ExtractedFiles' Extracts the contents of 'archive1.zip' and 'archive2.zip' (provided via pipeline) to the 'C:\ExtractedFiles' folder. .EXAMPLE $fileList = @( [pscustomobject]@{ Path = 'C:\Temp\archive1.zip' }, [pscustomobject]@{ Path = 'C:\Temp\archive2.zip' } ) $fileList | Expand-specZipFile -Destination 'C:\ExtractedFiles' Extracts the contents of 'archive1.zip' and 'archive2.zip' (provided as PSCustomObjects via pipeline) to the 'C:\ExtractedFiles' folder. (You can add a 'Destination' property to your PSCustomObjects if you want to specify a different destination folder for each zip file.) .NOTES Author: owen.heaume Version: 1.0.0 Changelog: - Initial version #> [CmdletBinding()] param ( [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string[]]$Path, [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [string]$Destination ) begin { } process { foreach ($item in $Path) { try { Expand-Archive -Path $item -DestinationPath $Destination -Force -ea stop Write-Host "Extracted $item to $Destination" -ForegroundColor DarkGray } catch { Write-Warning "Failed to extract $item to $Destination" [pscustomobject]@{ Path = $item Destination = $Destination Error = $_.Exception.Message } } } } end {} } |