PowerTip: Use PowerShell to Create Zip Archive

PowerTip: Use PowerShell to Create Zip Archive

  • Comments 5
  • Likes

Summary: Learn how to use Windows PowerShell to easily create a zip archive.

Hey, Scripting Guy! Question How can I use Windows PowerShell to create a zip archive of a folder on my system?

Hey, Scripting Guy! Answer Use Get-ChildItem from PSCX to copy the files, and pipe the output to the Write-Zip cmdlet:

Get-Childitem c:\fso -Recurse | Write-Zip -IncludeEmptyDirectories -OutputPath C:\fso_bu\fso.zip

Note: I have written several blog posts about the Windows PowerShell Community Extension Project.
For more information about obtaining the PSCX module, see these Hey, Scripting Guy! Blog posts.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Is it worth using 7zip command line utility for better compression vs doing it this way?

  • @The Duder
    The immediate advantage to this solution over 7zip isn't so much for better compression but that you leverage a tool native to the Server (PowerShell) to achieve the same task.

    You can even do some cool things like produce an RTF document using Windows PowerShell without having to Install Microsoft Word on a Server.

    Sean
    Windows PowerShell MVP

  • So i am using pscx and the command write-bzip2 it creates a Progress bar that doesn't goes away. Is there a way to clean that up?

  • http://en.chongsoft.com">seo tools
    [url=http://en.chongsoft.com]wormposter[/url]
    http://en.chongsoft.com

  • @Sean Kearney
    Except that PSCX isn't "native". If you can distribute other files across the system with your script, you can just as easily bundle 7zip's command line tool.
    @The Duder
    I would say there's no advantage, personally. I use 7zip's command line for build automation on a number of projects, and I've been very satisfied. If the ability to generate files that can be unzipped anywhere is a concern, there's no reason 7zip can't generate a plain zip file for you. That said, if PowerShell includes a simple command to zip files in the base PowerShell, I'll probably stop using 7zip to avoid the extra file distribution even if the compression would have been better. (Disk space is cheap and network bandwidth is plentiful enough that the difference probably won't matter. My time dealing with those extra files isn't.)