Compress-Item

Compresses a file/directory using the DotNetZip library.

Syntax

Compress-Item [-Path] <String[]> [[-OutFile] <String>] [-UseShell] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]

Description

You can supply a destination file path, via the OutFile parameter. If the file doesn't exist, it is created. If it exists, use the -Force parameter to overwrite it.

Each item added to the ZIP file will be added to the root of the file, with a name matching the original file's/directory's name. For example, if adding the file C:\Projects\Carbon\RELEASE NOTE.txt, it would get added to the ZIP file as RELEASE NOTES.txt.

If you don't supply an output file path, one will be created in the current user's TEMP directory.

A System.IO.FileInfo object is returned representing the ZIP file. If you're using the WhatIf switch, nothing is returned.

Microsoft's DSC Local Configuration Manager is unable to unzip files compressed with the DotNetZip library (or the ZipFile class in .NET 4.5), so as an alternative, if you specify the UseShell switch, the file will be compressed with the Windows COM shell API.

Related Commands

Parameters

Name Type Description Required? Pipeline Input Default Value
Path String[]

The path to the files/directories to compress.

true true (ByPropertyName)
OutFile String

Path to destination ZIP file. If not provided, a ZIP file will be created in the current user's TEMP directory.

false false
UseShell SwitchParameter

Uses the Windows COM shell API to create the zip file instead of the DotNetZip library. Microsoft's DSC Local Configuration Manager can't unzip files zipped with DotNetZip (or even the .NET 4.5 ZipFile class).

false false False
Force SwitchParameter

Overwrites an existing ZIP file.

false false False
WhatIf SwitchParameter false false
Confirm SwitchParameter false false
CommonParameters This cmdlet supports common parameters. For more information type
Get-Help about_CommonParameters.

Return Values

System.IO.FileInfo.

EXAMPLE 1

Compress-Item -Path 'C:\Projects\Carbon' -OutFile 'C:\Carbon.zip'

Demonstrates how to create a ZIP file of the C:\Projects\Carbon directory.

EXAMPLE 2

Get-ChildItem -Path 'C:\Projects\Carbon' | Where-Object { $_.PsIsContainer} | Compress-Item -OutFile 'C:\Projects\Carbon.zip'

Demonstrates how you can pipe items to Compress-Item for compressing.

EXAMPLE 3

Compress-Item -Path 'C:\Projects\Carbon' -OutFile 'C:\Carbon.zip' -UseShell

Demonstrates how to create a ZIP file with the Windows shell COM APIs instead of the DotNetZip library.