Search notes:

PowerShell cmdLet Compress-Archive

The PowerShell cmdLet compress-archive can be used to create zip files.
The maximum size of zip files that compress-archive can create is 2MB (which is a limitation of the class System.Io.Compression.ZipFile that compress-archive internally uses).

-path vs -literalPath

compress-archive has the two parameters -path and -literalPath to specifiy the files to be compressed.
They differ in that -literalPath does not expand (or accept) wild cards characers (* and ?)..
Both parameters accept an array of strings.
In order to use -literalPath, it needs to be specified explicitly. (-path is the default (and first) parameter for compress-archive).

-compressionLevel

The parameter -compressionLevel can be given one of three options:

-update / -force

If the specified zip file already exists, -update or -force determine what happens to the zip file.
-force will create a new zip file.
-update adds the specified files to the zip files. If they're already in the zip file, they're updated if the zip's version of the file is older than that with which they're updated.

Example

The following command creates project.zip that contains all view-*.sql, table-*.sql files that are found in the current directory, plus the readme.txt file that also needs to be in the current directory.
The command uses the line continuation character (backtick) to make it visually easier for the read to see what's being added.
Note that the first parameter by default is -path which accepts an array of strings. So, the commas (before table-*.sql and readme.txt) are necessary to tell compress-archive that they belong to this parameter.
project.zip is not prepended by a comma, which indicates that this is the second parameter.
PS C:\> compress-archive    `
   view-*.sql               `
 , table-*.sql              `
 , readme.txt               `
   project.zip              `
  -force

Compress an entire folder

The following creates a zip archive for the entire folder data-2023-04. In the zip file, data-2023-04 will be the top level folder:
PS> compress-archive  data-2023-04   data-2023-04.zip
With \* added to the folder name, the «top level» folder of the source data (data-2023-04) does not show up in the zip file:
PS> compress-archive  data-2023-04\* data-2023-04.zip

See also

expand-archive
My PowerShell module zip.
Powershell command noun: archive

Index