I would like to make a powershell script using this or the compress-archiev in powershell to make a zip file with only files, not folders. Top folder contaning folders, in subfolders there are folders contaning files. I want to only zip the files not the folders for each folder in the top level folder.

What I have zips all the folders C:\some\folder01\manyfolders\files but i need only the C:\some\folder01\files and C:\some\folder02\files

Add-Type -AssemblyName System.IO.Compression.FileSystem

$directory = "C:\some\folder\manyfolders\"

$folders = Get-ChildItem $directory -Recurse | Where-Object {$_.PSIsContainer} | Select-Object -ExpandProperty FullName

foreach ($folder in $folders) {
$archive = Get-ChildItem $folder -Recurse -File
[System.IO.Compression.ZipFile]::CreateFromDirectory($folder, $archive, 'Optimal', $True)
  • 2
    You should leave your current script in the question. It shows that you actually tried something, which is what people here expect from you. And it allows others to tell you why it doesn't work as you intend it. – Gerald Schneider Apr 16 at 7:50
  • Currently my script is not working, so I removed it. I've uploaded it again. – hans hemrub Apr 16 at 7:57
  • I did provide you an answer but this question is not about servers in any way so it should be moved to the proper site. – Overmind Apr 16 at 8:06

The script is as simple as this:

Get-ChildItem $directory -Directory  |Foreach-Object { 
    Compress-Archive -Path ($_|Get-ChildItem -Recurse -File).FullName -Destination ($_.name + ".zip") 
  1. Get all directories in the given directory (without subdirectories)
  2. For each directory create a zip archive with the name of the directory with a list of all files inside that directory (recursive)
  • Doesn't that put each content in a separate file ? – Overmind Apr 16 at 8:46
  • It does. I read the question that it was intended that way. Without archives per directory it gets even simpler. – Gerald Schneider Apr 16 at 8:48
  • I remember old archivers having actually options not to store any path and just put all the files together but to do it from a command line creates some dependencies. I deleted my answer since it does no longer fit the requirement. You should specify that this only works from v4.5 above. – Overmind Apr 16 at 8:49
  • I can see how my question creates some confusion, for this I am sorry. However I want alle files from subfolders to be added in a zip without the subfolders, only the files from them. – hans hemrub Apr 16 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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