PoshCode Logo PowerShell Code Repository

Name: 680581.ps1

Goes one directory deep into the $RootPath and then recurses through the subdirectories for any zip files, extracting them to a common location.

  1. Param(
  2.     [Parameter(Mandatory=$true)][string]$RootPath,
  3.     [Parameter(Mandatory=$true)][string]$Destination
  4. )
  5.  
  6. function Expand-ZipFile {
  7.     Param(
  8.         [Parameter(Mandatory=$true)][string]$File,
  9.         [Parameter(Mandatory=$true)][string]$Path
  10.     )
  11.     [System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | Out-Null
  12.     [System.IO.Compression.ZipFile]::ExtractToDirectory($File, $Path)
  13. }
  14.  
  15. $subFolders = gci -Path $RootPath -Directory
  16. foreach($subFolder in $subFolders) {
  17.     $zipFiles = gci -Path $subFolder.FullName -Recurse -Include "*.zip" -File
  18.     foreach($zipFile in $zipFiles) {
  19.         $zipFilePath = $zipFile.FullName
  20.         Write-Host "Extracting $zipFilePath"
  21.         Expand-ZipFile -File $zipFilePath -Path $Destination
  22.     }
  23. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me