Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I move log files to zip archives with System.IO.Compression.FileSystem in PowerShell?

I have more folders with log files per application:



and so on.. And I want to process these files into a zip archive per day.
share|improve this question

I don't see why would you want to move the files specifically. You can always compress them, verify that it's done correctly and remove the files in the end.

In order to get this working in Powershell you need the .NET 4.5 CLR since that class is new in that version.

# load the assembly required
$sourceFolder = "C:\Path\To\Your\Logs"
$destinationFile = "C:\Path\To\Your\"    
# desired compression level (Optimal, Fastest or NoCompression)
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
# include the directory $sourceFolder or just it's contents
$includeBaseDirectory = $false

[System.IO.Compression.ZipFile]::CreateFromDirectory($sourceFolder, $destinationFile, $compressionLevel , $includeBaseDirectory)

If you get Unable to find type [System.IO.Compression.ZipFile]: make sure that the assembly containing this type is loaded. after the last method call it means you don't have .NET 4.5 CLR installed (or the wrong assembly is loaded from GAC).

share|improve this answer
I want to create ZIP from multiple files from multiple folders :) And I don't want to create temporary folders. I think it's possible somehow with manipulating the files full path in an PSObject for example. – gazsiazasz May 9 '13 at 12:25

Assuming you have .NET 4.5 CLR you want to use the CreateEntryFromFile method to build up your zip files one log file at a time.

share|improve this answer

Using the Shell.Application object will do away with temp files. Additionally you do not need to worry if NET 4.5 is installed. This object is available since Windows XP.

The below code assumes you have all you application log folders are under a single folder. It will look for log files matching the date (today), create a zip file and copy the files into it. The zip file will be named according to the current date too.

$date_string = Get-Date -Format 'yyyyMMdd'

$zip_filename = "logs_$"

# Create the empty zip file
Set-Content $zip_filename ( [byte[]] @( 80, 75, 5, 6 + (, 0 * 18 ) ) ) -Encoding Byte

$zip_file = Get-Item -Path $zip_filename | ForEach-Object { 

    (New-Object -ComObject Shell.Application).NameSpace($_.FullName) 

[array]$log_files = Get-ChildItem -Recurse -Filter "*_$date_string.log"

for ($i=1; $i -le $log_files.Count; $i++) {


    # Copying is async so we need to check if it is done before continuing.
    while ($zip_file.Items().Count -ne $i) { sleep 0.1 }


My assumptions might be wrong, and you might need to make some changes to make it work for your environment. The key takeaway from this example is using the Shell.Application object to create zip files.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.