Compress-Archive (PowerShell 5+)

Create a new archive, or zipped file, from specified files and folders.

Syntax
      Compress-Archive [-Path] String[] [-DestinationPath] String
         [-CompressionLevel String ] [-Update]
            [-Confirm] [-WhatIf] [CommonParameters]

      Compress-Archive [-DestinationPath] String -LiteralPath String[]
         [-CompressionLevel String ] [-Update]
            [-Confirm] [-WhatIf] [CommonParameters]

key
   -CompressionLevel String
       How much compression to apply when creating the archive file.
       Faster compression requires less time to create the file, but can result in larger file sizes.
       The default value is Optimal. valid values for this parameter:
         -- 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.

   -DestinationPath String
       The path to the archive output file. (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 DestinationPath does not include a .zip file name extension, one will be added.

   -LiteralPath String[]
       The path (or paths) to the files 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 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[]
       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
       Update the specified archive replacing older versions of files in the archive with newer
       versions of files that have the same names. This can be used to add files to an existing archive.

   -Confirm
       Prompt for confirmation before executing the command.

   -WhatIf
       Describe what would happen if you executed the command without actually executing the command.

Examples

Create a new archive file:

PS C:\> Compress-Archive -LiteralPath C:\demo\Draft.doc,
C:\demo\final.doc -CompressionLevel Optimal -DestinationPath C:\output\new.Zip

Create an archive (new.zip) from an entire folder (the .zip extension is added automatically):

PS C:\> Compress-Archive -Path C:\demo -DestinationPath C:\output\new

Create a new archive called Multi.zip, containing multiple files specified with a wildcard character:

PS C:\> Compress-Archive -Path C:\demo\* -CompressionLevel Fastest -DestinationPath C:\output\multi.zip

Update an existing archive file:

PS C:\> Compress-Archive -Path C:\demo\* -Update -DestinationPath C:\output\Draft.Zip

Group some files by their 6 char prefix and then zip the grouped files into an archive named after the group [via StackOverflow]:

Get-ChildItem D:\demo | Group-Object -Property {
   $_.Name.Substring(0, 6)
} | ForEach-Object {
   Compress-Archive -Path $_.Group -DestinationPath "$($_.Name).zip"
}

“The ability to reduce everything to simple fundamental laws does not imply the ability to start from those laws and reconstruct the universe” ~ Philip Warren Anderson

Related PowerShell Cmdlets

Expand-Archive - Extract files from an archive (zipped) file.
New-Zipfile - for PowerShell 3/4.


 
Copyright © 1999-2024 SS64.com
Some rights reserved