PowerTip: Use PowerShell 5 to Compress Files

Summary: Learn how to use the new Compress-Archive cmdlet to compress selected files in Windows PowerShell 5.0.

Hey, Scripting Guy! Question How can I compress a bunch of files on my computer that match a pattern I supply to Get-ChildItem?

Hey, Scripting Guy! Answer Use Get-ChildItem to find the files you want to compress, and pipe the results to the Compress-Archive cmdlet,
           for example (gci is an alias for Get-ChildItem):

gci c:\fso\*.txt | Compress-Archive -DestinationPath c:\fso\myarchive.zip

Comments (2)

  1. Dendory says:

    And this works for earlier versions of PS:

    Add-Type -AssemblyName System.IO.Compression.FileSystem
    $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    [System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir, $zipfilename, $compressionLevel, $false)

  2. Anders P says:

    CreationTime, LastAccessTime and LastWriteTime are not preserved using Compress-Archive.
    If you think that matters, please vote for it here:

Skip to main content