locked
Powershell function: Unzip all files in a folder RRS feed

  • Question

  • I have a folder "Zipped" which contains 100s of .zip files

    I want to unzip all zip-files in this folder to "\\zipped\Unzipped" folder.

    It seems expand-archive cmdlet only support one file at a time.
    So do you need to create an array with all filenames and then create a loop to unzip all files?

    Wednesday, December 6, 2017 11:37 AM

All replies

  • Get-ChildItem *.zip | Expand-Archive

    Read the help for both CmdLets.


    \_(ツ)_/

    Wednesday, December 6, 2017 11:46 AM
  • Get-ChildItem 'path to folder' -Filter *.zip | Expand-Archive -DestinationPath 'path to extract' -Force

    This should work.
    Wednesday, December 6, 2017 11:51 AM
  • This one I used as well, multiple times. Worked perfectly.


    PARAM (
        [string] $ZipFilesPath = "X:\Somepath\Full\Of\Zipfiles",
        [string] $UnzipPath = "X:\Somepath\to\extract\to"
    )
     
    $Shell = New-Object -com Shell.Application
    $Location = $Shell.NameSpace($UnzipPath)
     
    $ZipFiles = Get-Childitem $ZipFilesPath -Recurse -Include *.ZIP
     
    $progress = 1
    foreach ($ZipFile in $ZipFiles) {
        Write-Progress -Activity "Unzipping to $($UnzipPath)" -PercentComplete (($progress / ($ZipFiles.Count + 1)) * 100) -CurrentOperation $ZipFile.FullName -Status "File $($Progress) of $($ZipFiles.Count)"
        $ZipFolder = $Shell.NameSpace($ZipFile.fullname)
     
     
        $Location.Copyhere($ZipFolder.items(), 1040) # 1040 - No msgboxes to the user - http://msdn.microsoft.com/en-us/library/bb787866%28VS.85%29.aspx
        $progress++
    }


    • Proposed as answer by Lilth Wednesday, December 6, 2017 11:56 AM
    Wednesday, December 6, 2017 11:53 AM
  • Hi BrendenT,

    when you cobinate the cmdlets get-childitem with expand you can reach you goal.

    Get-ChildItem \\zipped\*zip | Expand-Archive -DestinationPath \\zipped\unzipped\

    All Zip-Files would be extract to the folder unzipped.

    regards patrick

    Wednesday, December 6, 2017 12:20 PM
  • Here the simple code to do the things:

    Assume that your zip file have same file name in all zip files, the below one will overwrite all the files which has same file name

    Get-ChildItem \\zipped\*zip | Expand-Archive -DestinationPath \\zipped\unzipped\

    I hope the below one will help you reach your goal

    #Extract zip files to the same location with same folder name (removed .zip)

    gci -Recurse -Filter *.zip |ForEach-Object {$n=($_.Fullname.trimend('.zip')); Expand-Archive -Path $_.Fullname -DestinationPath $n -Force}

    #Extract zip files to different location with same name (extracted folder name will contains .zip)

    gci -Filter *.zip |ForEach-Object {Expand-Archive -Path $_ -DestinationPath C:\Temp\$_ -Force}

    #Extract zip files to different folder location, this will extract the zip files including sub folders in destination)

    PS C:\Temp\tt> gci -Recurse -Filter *.zip |ForEach-Object {$n=($_.Fullname.trim('C:\Temp .zip')); Expand-Archive -Path $_.Fullname -DestinationPath C:\TempT\$n -Force}

    BR

    Rex02



    • Edited by Rex02 Saturday, November 30, 2019 4:42 PM
    Saturday, November 30, 2019 4:38 PM
  • Please fix your post and format the code as code: How to Use the Code Feature in a TechNet Forum Post

    Thanks


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Saturday, November 30, 2019 5:42 PM
  • Glad you clarified reading the get-help for the cmdlets. If executed as is it would make a very long nest of extracted directories. Not tough to navigate via PS but for GUI bound users, nightmare zone.
    Saturday, December 21, 2019 9:34 PM