Powershell - Compress-Archive and Expand-Archive to create and extract ZIP archives

To support me, you can subscribe to the channel, share and like the videos, disable your ad blocker or make a donation. Thank you!

Windows Powershell, via the Microsoft.PowerShell.Archive module, allows us to create, update and extract ZIP archives.

The Compress-Archive command archive in ZIP format only, does not support files larger than 2GB and does not support hidden files.

Version 2 of the Microsoft.PowerShell.Archive module has obviously been abandoned :
https://github.com/PowerShell/Microsoft.PowerShell.Archive

# Compress a folder and its contents by specifying the compression level (NoCompression, Fastest, Optimal).
# Optimal is the default
Compress-Archive -Path c:\test -DestinationPath c:\test.zip -CompressionLevel Fastest
# Compress the contents of a folder
Compress-Archive -Path c:\test\* -DestinationPath c:\test.zip
# Compress the contents of a folder and overwrite a previous archive (-Force)
Compress-Archive -Path c:\test\* -DestinationPath c:\test.zip -Force
# Compress certain files
Compress-Archive -Path c:\test\doc1.txt,c:\test\*.docx -DestinationPath c:\test.zip
# Compress certain files using Get-ChildItem to filter
Get-ChildItem -Path c:\test -file | Compress-Archive -DestinationPath c:\test.zip
# Update the contents of the archive (newer files replace older ones in the archive)
Compress-Archive -Path c:\test -DestinationPath c:\test.zip -Update
# Uncompress an archive in a folder with the name of the archive in the current folder
Expand-Archive -Path c:\test.zip
# Unzip an archive into a folder specifying the location (if the folder does not exist, it will be created)
Expand-Archive -Path c:\test.zip -DestinationPath c:\test2
# Unzip an archive into a folder with the archive name in the current folder, overwriting existing files
Expand-Archive -Path c:\test.zip -Force
# Display the contents of an archive
[System.IO.Compression.ZipFile]::OpenRead(‘c:\test.zip’).Entries.fullname
# To take hidden files into account, you can pass directly through .net, but the file will lose its archive attribute
[System.IO.Compression.ZipFile]::CreateFromDirectory(‘c:\test’,‘c:\test.zip’)

Related links