Compress-Archive
Creates a compressed archive, or zipped file, from specified files and folders.
Syntax
Path (Default)
Compress-Archive
[-Path] <String[]>
[-DestinationPath] <String>
[-CompressionLevel <String>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
PathWithUpdate
Compress-Archive
[-Path] <String[]>
[-DestinationPath] <String>
[-CompressionLevel <String>]
[-Update]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
PathWithForce
Compress-Archive
[-Path] <String[]>
[-DestinationPath] <String>
[-CompressionLevel <String>]
[-Force]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
LiteralPathWithUpdate
Compress-Archive
[-DestinationPath] <String>
-LiteralPath <String[]>
[-CompressionLevel <String>]
[-Update]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
LiteralPathWithForce
Compress-Archive
[-DestinationPath] <String>
-LiteralPath <String[]>
[-CompressionLevel <String>]
[-Force]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
LiteralPath
Compress-Archive
[-DestinationPath] <String>
-LiteralPath <String[]>
[-CompressionLevel <String>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Description
The Compress-Archive cmdlet creates a compressed, or zipped, archive file from one or more
specified files or folders. An archive packages multiple files, with optional compression, 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.
The Compress-Archive cmdlet uses the Microsoft .NET API System.IO.Compression.ZipArchive
to compress files. The maximum file size is 2 GB because there's a limitation of the underlying API.
Some examples use splatting to reduce the line length of the code samples. For more information, see about_Splatting.
Examples
Example 1: Create an archive file
This example creates a new archive file, Draft.zip, by compressing two files, Draftdoc.docx and
diagram2.vsd, specified by the Path parameter. The compression level specified for this
operation is Optimal.
$compress = @{
Path= "C:\Test\Reference\Draftdoc.docx", "C:\Test\Reference\Images\diagram2.vsd"
CompressionLevel = "Optimal"
DestinationPath = "C:\Test\Archives\Draft.Zip"
}
Compress-Archive @compress
Example 2: Create an archive file using LiteralPath
This example creates a new archive file, Draft.zip, by compressing two files, Draft doc.docx and
Diagram [2].vsd, specified by the LiteralPath parameter. The compression level specified for
this operation is Optimal.
$compress = @{
LiteralPath= "C:\Test\Reference\Draft Doc.docx", "C:\Test\Reference\Images\Diagram [2].vsd"
CompressionLevel = "Optimal"
DestinationPath = "C:\Test\Archives\Draft.Zip"
}
Compress-Archive @compress
Example 3: Create an archive with wildcard characters
This example creates a new archive file, Draft.zip, in the C:\Archives folder. The new archive
file contains every file, folder, and subfolder in the C:\Reference root folder, because an
asterisk (*) wildcard character was used in place of specific file names in the Path
parameter. The root folder, Reference, is excluded from the archive.
Compress-Archive -Path C:\Reference\* -CompressionLevel Fastest -DestinationPath C:\Archives\Draft
Notice that the file name extension .zip wasn't added to the value of the DestinationPath
parameter. PowerShell appends the .zip extension to the file name automatically. The specified
compression level is Fastest. The faster compression level will compress large numbers of files
faster, but might result in a larger output file.
Note
To zip only the files in the specified root folder, use the star-dot-star (*.*) wildcard.
Example 4: Update an existing archive file
This example updates an existing archive file, Draft.Zip, in the C:\Archives folder.
Compress-Archive -Path C:\Reference\* -Update -DestinationPath C:\Archives\Draft.Zip
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.
Example 5: Create an archive from an entire folder
This example creates an archive from an entire folder, C:\Reference. The archive contains the root
folder, Reference, and all its files, folders, and subfolders.
Compress-Archive -Path C:\Reference -DestinationPath C:\Archives\Draft
Notice that the file name extension .zip wasn't added to the value of the DestinationPath
parameter. PowerShell appends the .zip extension to the file name automatically.
Parameters
-CompressionLevel
Specifies how much compression to apply when you're creating the archive file. Faster compression requires less time to create the file, but can result in larger file sizes.
If this parameter isn't specified, the command uses the default value, Optimal.
The acceptable values for this parameter are as follows:
- Fastest. Use the fastest compression method available to reduce processing time. Faster compression can result in larger file sizes.
- NoCompression. Doesn't compress the source files.
- Optimal. Processing time is dependent on file size.
Parameter properties
| Type: | String |
| Default value: | Optimal |
| Accepted values: | Optimal, NoCompression, Fastest |
| Supports wildcards: | False |
| DontShow: | False |
Parameter sets
(All)
| Position: | Named |
| Mandatory: | False |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
-Confirm
Prompts you for confirmation before running the cmdlet.
Parameter properties
| Type: | SwitchParameter |
| Default value: | False |
| Supports wildcards: | False |
| DontShow: | False |
| Aliases: | cf |
Parameter sets
(All)
| Position: | Named |
| Mandatory: | False |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
-DestinationPath
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, and specifies
either the absolute or relative path to the zipped file. If the file name specified in
DestinationPath doesn't have a .zip file name extension, the cmdlet adds a .zip file name
extension.
Parameter properties
| Type: | String |
| Default value: | None |
| Supports wildcards: | False |
| DontShow: | False |
Parameter sets
(All)
| Position: | 1 |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
-Force
Forces the command to run without asking for user confirmation.
Parameter properties
| Type: | SwitchParameter |
| Default value: | False |
| Supports wildcards: | False |
| DontShow: | False |
Parameter sets
PathWithForce
| Position: | Named |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
LiteralPathWithForce
| Position: | Named |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
-LiteralPath
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's typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose each escape character in single quotation marks, to instruct 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.
Parameter properties
| Type: | String[] |
| Default value: | None |
| Supports wildcards: | False |
| DontShow: | False |
| Aliases: | PSPath |
Parameter sets
LiteralPathWithUpdate
| Position: | Named |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | True |
| Value from remaining arguments: | False |
LiteralPathWithForce
| Position: | Named |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | True |
| Value from remaining arguments: | False |
LiteralPath
| Position: | Named |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | True |
| Value from remaining arguments: | False |
-Path
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.
To zip all the files, folders, and subfolders in a specified root folder, use the asterisk (*)
wildcard. The root folder is excluded from the archive.
For example:
-Path C:\Reference\*
To zip only the files in the specified root folder, use the star-dot-star (*.*) wildcard.
For example:
-Path C:\Reference\*.*
Parameter properties
| Type: | String[] |
| Default value: | None |
| Supports wildcards: | True |
| DontShow: | False |
Parameter sets
Path
| Position: | 0 |
| Mandatory: | True |
| Value from pipeline: | True |
| Value from pipeline by property name: | True |
| Value from remaining arguments: | False |
PathWithUpdate
| Position: | 0 |
| Mandatory: | True |
| Value from pipeline: | True |
| Value from pipeline by property name: | True |
| Value from remaining arguments: | False |
PathWithForce
| Position: | 0 |
| Mandatory: | True |
| Value from pipeline: | True |
| Value from pipeline by property name: | True |
| Value from remaining arguments: | False |
-Update
Updates the specified archive by replacing older file versions in the archive with newer file versions that have the same names. You can also add this parameter to add files to an existing archive.
Parameter properties
| Type: | SwitchParameter |
| Default value: | False |
| Supports wildcards: | False |
| DontShow: | False |
Parameter sets
PathWithUpdate
| Position: | Named |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
LiteralPathWithUpdate
| Position: | Named |
| Mandatory: | True |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
-WhatIf
Shows what would happen if the cmdlet runs. The cmdlet isn't run.
Parameter properties
| Type: | SwitchParameter |
| Default value: | False |
| Supports wildcards: | False |
| DontShow: | False |
| Aliases: | wi |
Parameter sets
(All)
| Position: | Named |
| Mandatory: | False |
| Value from pipeline: | False |
| Value from pipeline by property name: | False |
| Value from remaining arguments: | False |
CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.
Inputs
String
You can pipe a string that contains a path to one or more files.