On Windows, you can use the built-in PowerShell cmdlets to zip and unzip files from the command prompt or from PS1 scripts. Starting with PowerShell 5.0 (introduced in Windows 10), the Compress-Archive and Expand-Archive cmdlets are available for working with ZIP archives. These cmdlets are available in Windows PowerShell 5.0+ and PowerShell 7+.
Note. Quotation marks are required around the file path if the source or destination file name contains a space.
Options for the Compress-Archive cmdlet
DestinationPath โ path to ZIP archive
Path โ the name of the file or folder you want to compress
Force โ overwrite the file if an archive with the same name already exists
CompressionLevel โ allows you to set the compression level. Possible values: NoCompression, Fastest, and Optimal (used by default)
If you want to zip only the directory contents (including subfolders), but not the directory itself, specify:
-Path C:\temp\*
In case you need the ZIP archive to contain the folder itself (instead of only its contents), you should specify the folder path without the wildcard character:
Note that Compress-Archive ignores hidden files and folders when creating/updating ZIP archives. This behavior also applies to current PowerShell 7 versions. If you need to include hidden or system files, you need to use the .NET System.IO.Compression API or a third-party archiving tool (such as 7-Zip).
If the source file or folder name contains wildcard characters (such as [ ], *, or ?), you can use the -LiteralPath parameter instead of -Path to prevent PowerShell from interpreting them as wildcard patterns:
Note that Compress-Archive has a limitation when adding very large individual files to a ZIP archive. In particular, files larger than 2 GB can cause the cmdlet to fail with a Stream was too long error because of limitations in the underlying .NET System.IO.Compression API. This limitation applies to both Windows PowerShell 5.1 and current PowerShell 7 versions. If you need to archive individual files larger than 2 GB, consider using 7-Zip/another archiving tool that supports large files.
Keep in mind that Compress-Archive uses the .NET System.IO.Compression ZIP implementation. The relevant limitation affects the size of an individual file being added to the archive, rather than the total size of the ZIP archive.
If you try to create a large ZIP archive file, in some cases the Compress-Archive cmdlet will return an error:
MethodInvocationException: Exception calling “Write” with “3” argument(s): “Stream was too long.”
Archive multiple directories at once
To archive multiple directories at once, specify a list of folders separated by commas in the Path parameter:
Compress-Archive also accepts file objects from the PowerShell pipeline. For example, in order to archive all log files from a folder, run the following command:
In some cases, you may need to archive a folder but exclude specific file types/files. For example, to exclude temporary files, use the following command:
Add the -Force parameter if you want to overwrite files in the destination directory.
The -Force parameter overwrites existing files in the destination folder. This makes it useful for restoring/updating previously extracted content. Here is an example:
Note. Keep in mind that Expand-Archive always extracts the entire archive (selective extraction of individual files/folders is not supported). If you need to extract specific archive contents, you can use the .NET System.IO.Compression.ZipFile API/third-party tools (such as 7-Zip).
Common Errors
When working with Compress-Archive and Expand-Archive, you may encounter the following errors:
Error
Possible Cause
Recommendation
The archive file format is invalid.
The ZIP file is corrupted/not a valid ZIP archive.
Verify the archive integrity/recreate the ZIP file.
The file already exists.
The destination archive/extracted file already exists.
Use the -Force parameter to overwrite existing files if appropriate.
Access to the path is denied.
Insufficient permissions to read or write the specified path.
Run PowerShell with appropriate privileges or verify NTFS/share permissions.
Cannot find path.
The source file/destination directory does not exist.
Check if the specified path is correct and create the destination folder if necessary.
These are built-in PowerShell cmdlets used to create and extract ZIP archives directly from the command line or scripts. They are available in PowerShell 5.0+ (Windows 10+) and PowerShell 7+.
Yes, large archives may cause errors like โStream was too longโ depending on PowerShell/.NET version. For very large archives, tools like 7-Zip are recommended.
Not always. Depending on PowerShell version and context, hidden/system files may not be consistently included, so itโs recommended to verify archive contents.
I enjoy technology and developing websites. Since 2012 I'm running a few of my own websites, and share useful content on gadgets, PC administration and website promotion.