Forum Discussion
johnmsch
Nov 16, 2023Copper Contributor
Compress-Archive Not Working?
I'm running Powershell version 5.1.17763.4974 on a server with Windows Server 2019 Datacenter v10. Been using PS for years but trying Compress-Archive for the first time. Tried building a script wh...
LainRobertson
Nov 17, 2023Silver Contributor
Hey, John.
Yes, you certainly can manipulate an archive, however, you have to do so from .NET. Here's a very basic example, which you can easily extend as it's a pretty simple set of classes to work with.
Example
# Add the required .NET library reference.
Add-Type -AssemblyName "System.IO.Compression.FileSystem";
# Open the Zip file.
$Archive = [System.IO.Compression.ZipFile]::Open("<full path to your Zip file>", [System.IO.Compression.ZipArchiveMode]::Update);
# Delete the first entry.
$Archive.Entries[0].Delete();
# Finish up properly by disposing the $Archive object.
$Archive.Dispose();
In a real-world context, you'd most likely enumerate the Entries collection and make smarter decisions based on file size, type, etc.
References
- ZipFile Class (System.IO.Compression) | Microsoft Learn
- ZipArchive Class (System.IO.Compression) | Microsoft Learn
- ZipArchiveEntry Class (System.IO.Compression) | Microsoft Learn
Cheers,
Lain
johnmsch
Nov 17, 2023Copper Contributor
Thanks again for all you help LainRobertson
Hope you have a wonderful weekend!