Easily Unzip Multiple Zips Using PowerShell

Easily unzip multiple zips using powershell

Problem: Unzip mulitple files in one go.

PowerShell

PowerShell is a task automation and configuration management program from Microsoft, consisting of a command-line shell and the associated scripting language

I started working on PowerShell years back when I started with dev ops to automate simple tasks, like packaging the application products, automated builds, etc.

When I hit a problem like unzipping a huge number of files, my very first thought was to select all the files and right-click and select unzip, but this did not work. That's when I landed on the below line to unzip bulk.

$files = Get-Childitem .
foreach($fs in $files){Expand-Archive $fs -DestinationPath .}

The file line reads all the files in the current directory (. implies current directory, you can also give full directory path)

The second line is using PowerShell "Expand-Archive" command to unzip all the zip files in the list.

Expand-Archive
      [-Path] <String>
      [[-DestinationPath] <String>]
      [-Force]
      [-PassThru]
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]

Steps

  1. Save the commands mentioned in ps1 file: myscript.ps1
  2. Open windows PowerShell and run the script file: .\myscript.ps1

If this fails, then set the execution policy to unrestricted for this session and try again.

Conclusion

If you are using a windows machine and need a quick way to unzip files, then go with expand-archive.

References

  1. https://learn.microsoft.com/en-us/powershell/
  2. https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.archive/expand-archive?view=powershell-7.3