23

Can I somehow exclude a folder when I compress an archive like this?

$compress = Compress-Archive $DestinationPath $DestinationPath\ARCHIVE\archiv-$DateTime.zip -CompressionLevel Fastest

Now it always saves the whole folder structure of $destinationpath to the archive, but since the archive is in the same folder, it always gets zipped into a new archive, making the archive double in size every time I run the command.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Balthazar
  • 463
  • 2
  • 5
  • 13
  • 1
    set the destination to outside of the directory and once compression is complete the move it in. or select the contents of the destination excluding the archive and then compress that – Nkosi Dec 10 '16 at 23:47

3 Answers3

31

Get all the files you want to compress, excluding the files and folders you don't want compressed and then pass that to the cmdlet

# target path
$path = "C:\temp"
# construct archive path
$DateTime = (Get-Date -Format "yyyyMMddHHmmss")
$destination = Join-Path $path "ARCHIVE\archive-$DateTime.zip"
# exclusion rules. Can use wild cards (*)
$exclude = @("_*.config","ARCHIVE","*.zip")
# get files to compress using exclusion filer
$files = Get-ChildItem -Path $path -Exclude $exclude
# compress
Compress-Archive -Path $files -DestinationPath $destination -CompressionLevel Fastest
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • 4
    The exclusion rules take effect only on the root folder, the wildcard rules will not take effect on subfolders. In this example Subfolder\xxx.zip will be zipped into the destination file. – daniol Aug 11 '20 at 14:55
  • This completely breaks the directory structure, though – KingRidgehead Oct 03 '22 at 16:13
22

you can use -update option of Compress-Archive. Select your subdirs with Get-ChildItem and Where

like it:

$YourDirToCompress="c:\temp"
$ZipFileResult="C:\temp10\result.zip"
$DirToExclude=@("test", "test1", "test2")

Get-ChildItem $YourDirToCompress -Directory  | 
           where { $_.Name -notin $DirToExclude} | 
              Compress-Archive -DestinationPath $ZipFileResult -Update
Esperento57
  • 16,521
  • 3
  • 39
  • 45
  • 3
    So there isn't like a simple argument like `--exclude myfile` like in Linux? – basickarl Sep 23 '19 at 12:20
  • there is, you can do: `gci -exclude *ps1 | compress-archive` – 4c74356b41 Aug 25 '21 at 08:07
  • 1
    - Directory only gets directories inside temp folder, so files inside temp folder are not present inside the final zip, and also you cannot exclude subfolders of subfolders in this way – Anand May 04 '22 at 21:38
1

I know this question is rather old, but wanted to post my solution here. This solution has worked for me and I hope it may help someone else having the same issue.

I took the ideas from the previous answers and developed them a bit.

So generally speaking what you need to do is to create two lists, one for the files in the root directory and another one for directories (excluding the directory you'd want to omit). Then you need to concatenate these two lists together and put them into -Path parameter of Compress-Archive cmdlet.

Voila! It will create a .zip archive with all files and directories we need, preserving the directory structure.

$files = Get-ChildItem -Path /RootDir -File 

$directories = Get-ChildItem -Path /RootDir -Recurse -Directory -Exclude DirToExclude

Compress-Archive -Path $($files + $directories) -DestinationPath Archive.zip
ssurba
  • 99
  • 2
  • 4