Compress-Archive
Compress-Archive [-Path*] <String[]> [-DestinationPath*] <String> [-CompressionLevel {Optimal | NoCompression |Fastest}] [-Confirm] -Force* [-WhatIf] [<CommonParameters>]
Compress-Archive [-DestinationPath*] <String> [-CompressionLevel {Optimal | NoCompression | Fastest}] [-Confirm]-Force* -LiteralPath* <String[]> [-WhatIf] [<CommonParameters>]
Compress-Archive [-DestinationPath*] <String> [-CompressionLevel {Optimal | NoCompression | Fastest}] [-Confirm]-LiteralPath* <String[]> -Update* [-WhatIf] [<CommonParameters>]
Compress-Archive [-DestinationPath*] <String> [-CompressionLevel {Optimal | NoCompression | Fastest}] [-Confirm]-LiteralPath* <String[]> [-WhatIf] [<CommonParameters>]
Compress-Archive [-Path*] <String[]> [-DestinationPath*] <String> [-CompressionLevel {Optimal | NoCompression |Fastest}] [-Confirm] [-WhatIf] [<CommonParameters>]
Compress-Archive [-Path*] <String[]> [-DestinationPath*] <String> [-CompressionLevel {Optimal | NoCompression |Fastest}] [-Confirm] -Update* [-WhatIf] [<CommonParameters>]
The Compress-Archive cmdlet creates a zipped (or compressed) archive file from one or more specified files or folders. An archive file allows multiple files to be packaged, and optionally compressed, into a single zipped file for easier distribution and storage. An archive file can be compressed by using the compression algorithm specified by the CompressionLevel parameter.
Because Compress-Archive relies upon the Microsoft .NET Framework API System.IO.Compression.ZipArchive to compress files, the maximum file size that you can compress by using Compress-Archive is currently 2 GB. This is a limitation of the underlying API.
Parameters
-CompressionLevel <String>
- Default value is None
- Accepts pipeline input False
Specifies how much compression to apply when you are creating the archive file. Faster compression requires less time to create the file, but can result in larger file sizes. The acceptable values for this parameter are:
- Fastest. Use the fastest compression method available to decrease processing time; this can result in larger file sizes. - NoCompression. Do not compress the source files. - Optimal. Processing time is dependent on file size.
If this parameter is not specified, the command uses the default value, Optimal.
-Confirm [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Prompts you for confirmation before running the cmdlet.
-DestinationPath <String>
- This value is required
- Default value is None
- Accepts pipeline input False
Specifies the path to the archive output file. This parameter is required. The specified DestinationPath value should include the desired name of the output zipped file; it specifies either the absolute or relative path to the zipped file. If the file name specified in DestinationPath does not have a .zip file name extension, the cmdlet adds a .zip file name extension.
-Force [<SwitchParameter>]
- This value is required
- Default value is False
- Accepts pipeline input False
@{Text=}
-LiteralPath <String[]>
- This value is required
- Default value is None
- Accepts pipeline input ByPropertyName
Specifies the path or paths to the files that you want to add to the archive zipped file. Unlike the Path parameter, the value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose each escape character in single quotation marks, to instruct Windows PowerShell not to interpret any characters as escape sequences. To specify multiple paths, and include files in multiple locations in your output zipped file, use commas to separate the paths.
-Path <String[]>
- This value is required
- Default value is None
- Accepts pipeline input ByPropertyName
Specifies the path or paths to the files that you want to add to the archive zipped file. This parameter can accept wildcard characters. Wildcard characters allow you to add all files in a folder to your zipped archive file. To specify multiple paths, and include files in multiple locations in your output zipped file, use commas to separate the paths.
-Update [<SwitchParameter>]
- This value is required
- Default value is False
- Accepts pipeline input False
Updates the specified archive by replacing older versions of files in the archive with newer versions of files that have the same names. You can also add this parameter to add files to an existing archive.
-WhatIf [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Shows what would happen if the cmdlet runs. The cmdlet is not run.
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable.
Inputs
System.StringOutputs
System.IO.FileInfoExamples
-
Create an archive file:
PS C:\> Compress-Archive -LiteralPath C:\Reference\Draftdoc.docx, C:\Reference\Images\diagram2.vsd -CompressionLevel Optimal -DestinationPath C:\Archives\Draft.Zip
This command creates a new archive file, Draft.zip, by compressing two files, Draftdoc.docx and diagram2.vsd, specified by the LiteralPath parameter. The compression level specified for this operation is Optimal.
-
Create an archive with wildcard characters:
PS C:\> Compress-Archive -Path C:\Reference\* -CompressionLevel Fastest -DestinationPath C:\Archives\Draft
This command creates a new archive file, Draft.zip, in the C:\Archives folder. Note that though the file name extension .zip was not added to the value of the DestinationPath parameter, Windows PowerShell appends this to the specified archive file name automatically. The new archive file contains every file in the C:\Reference folder, because a wildcard character was used in place of specific file names in the Path parameter. The specified compression level is Fastest, which might result in a larger output file, but compresses a large number of files faster.
-
Update an existing archive file:
PS C:\> Compress-Archive -Path C:\Reference\* -Update -DestinationPath C:\Archives\Draft.Zip
This command updates an existing archive file, Draft.Zip, in the C:\Archives folder. The command is run to update Draft.Zip with newer versions of existing files that came from the C:\Reference folder, and also to add new files that have been added to C:\Reference since Draft.Zip was initially created.
-
Create an archive from an entire folder:
PS C:\> Compress-Archive -Path C:\Reference -DestinationPath C:\Archives\Draft
This command creates an archive from an entire folder, C:\Reference. Note that though the file name extension .zip was not added to the value of the DestinationPath parameter, Windows PowerShell appends this to the specified archive file name automatically.
Additional Notes
This work is licensed under a Creative Commons Attribution 4.0 International. It is attributed to Microsoft Corporation and can be found here.
PowerShell Commands