PowerShell Script To Zip / Compress Files And Folders
Powershell allows you to zip / compress files easily from the command line or from a script. You can specify the parameters by name if you like or you could just specify them by position.
Zipp all the files in your current directory like this:
Compress-Archive * myArchive.zip
Compress a directory and everything in it like this
Compress-Archive testdir myArchive.zip
Compress only the contents of a directory but not the directory itself like this:
Compress-Archive testdir\* myArchive.zip
You can use a full path like this:
C:\test\test2\a\b\c\dir1
Or a relative path like this:
dir1
PowerShell Zip Files and Folders More Examples
Zip specific files:
Compress-Archive -LiteralPath c:\test\file1.txt c:\test\file2.txt, -DestinationPath myArchive.zip
Zip directory and contents:
Compress-Archive -LiteralPath dir1 -DestinationPath myArchive.zip
Zip the contents of a directory recursively but not the directory itself:
Compress-Archive -Path C:\dir1\subdir1\dir2\* -DestinationPath myArchive.zip
Only matching files:
Compress-Archive -Path C:\dir1\subdir1\dir2\*.jpg -DestinationPath C:\export\data\myArchive.zip
Update an existing archive:
Compress-Archive -Path C:\dir1\subdir1\dir2 -Update -DestinationPath C:\export\data\myArchive.zip
Unzip files with PowerShell
You can unzip files like this:
Expand-Archive .\myArchive.zip
Expand-Archive .\myArchive.zip dir1
Expand-Archive -LiteralPath myArchive.zip -DestinationPath dir1
Naming the parameters is optional.