Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm new to Powershell. I have a need that I assumed that it would be perfect for. However, I've quickly come to the point where I feel I'm in over my head. Basically, I need to create a .zip file.

I have a directory that looks like the following:


I need to create a .zip file named should NOT have all of the files inside of MyStuff. Instead, I need to include MyStuff/File1.txt, MyStuff/File2.txt, MyStuff/File3.txt, and MyStuff/Dir1/* into the .zip file. I have no idea how to do this with PowerShell.

I have PowerShell v1.0 installed on my machine. I've been trying "Write-Zip" from the PowerShell Community Extensions, however, I get an error that says: "The term 'Write-Zip' is not recognized as the name of a cmdlet, function, script file, or operable program".

What am I doing wrong?

share|improve this question
I'd start by suggesting that you upgrade powershell if you can, v1.0 is relatively old and I believe things have changed rather dramatically between then and now. That being said that error sounds like you just haven't loaded the extension correctly. –  Etan Reisner Jul 30 '13 at 18:48
As it turns out, I have version 3 installed. I've confirmed this by running $Host.version from the powershell prompt. For some reason, MS just kept it in the v1.0 direction :/ –  user26712 Jul 30 '13 at 19:27
How did you install/load the extension you are trying to use? Does Get-Command Write-Zip find the command? What about Get-Module <the extension module>? –  Etan Reisner Jul 30 '13 at 19:51
Get-Command Write-Zip and Get-Module pscx both work from the command line just fine. –  user26712 Jul 30 '13 at 20:28
So in a single powershell session you can run Get-Command Write-Zip and it finds the function but if you then try to run Write-Zip it fails with the error in your post? –  Etan Reisner Jul 30 '13 at 21:23

2 Answers 2

You need to download the Powershell Community Extension compatible with your installed Powershell version and install the same. Once the installation is done You need to move the PSCX module from the Program files location to the Powershell Module and then try using Write-Zip command.

share|improve this answer
Emphasis on "compatible with your installed version..." not sure what version of PSCX is compatible with PS v1.0, but it's not the latest one. –  Ryan Ries Aug 14 at 12:20

Zipping can be done using the CreateFromDirectory method of the .NET class System.IO.Compression.ZipFile. I prefer using .NET over installing community extensions and other add-ons, in order to keep the server environment as clean as possible.

I believe you cannot use it to add to a zip archive, but would need to temporarily copy the files and folders in the correct structure to a staging directory. From there you could create the zip file and delete the staging content.

To use .NET for zipping you first need to load the class assembly:


Then you can call the class:

[System.IO.Compression.ZipFile]::CreateFromDirectory($DirPath, $ZipFilePath, $CompressionLevel, $includeBaseDir)


$DirPath is the directory you want to zip.

$ZipFilePath is the name of the zipfile you want to create.

$Compressionlevel can be set to Optimal, Fastest or NoCompression.

$includeBaseDir can be $true or $false.

I wrote a function out of it to save me the bother of having to look up the .NET reference page all the time. If you look at the page you will see there is a method for unzipping too.

.NET 4.5 is required and I would strongly encourage you to upgrade powershell too. Every version has brought massive improvements. It also turns out using .NET is efficient, having benchmarked it against popular 3rd party zip applications it comes out strongly.

The function is pretty self explanatory, just make sure you run the function itself before calling it. Here is a short syntax rundown if you need it:

New-ZipFile {DirToZip} {ZipfilePathAndName} {fastORoptimalORnocompression} {IncludeBaseDirectoryInZip($true or $false)}

So to zip E:\stuff to D:\ using optimal compression and include the basedir .\stuff in the zipfile, you would run:

New-ZipFile 'E:\stuff' 'D:\' Optimal $true

Here is the function:

#region function New-ZipFile
   Creates a zip file from a directory.
   Creates a zip file from a directory. Options include compression level and wether or not to include the base directory. Class reference:
   New-ZipFile c:\dir c:\ Fastest $True
   New-ZipFile c:\dir c:\ Optimal $True
   Inputs to this cmdlet (if any)
   Output from this cmdlet (if any)
   General notes
   The component this cmdlet belongs to
   The role this cmdlet belongs to
   The functionality that best describes this cmdlet
function New-ZipFile
        # The directory to zip.

        # The zipfile name.

        # Specifies values that indicate whether a compression operation emphasizes speed or compression size.
        [ValidateSet("Fastest", "NoCompression", "Optimal", ignorecase=$True)]

        # $True to include the directory name from sourceDirectoryName at the root of the archive; $False to include only the contents of the directory.

    #Load the .NET 4.5 zip-assembly
    [System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | Out-Null
    # Zip the directory.
    [System.IO.Compression.ZipFile]::CreateFromDirectory($DirPath, $ZipFilePath, $CompressionLevel, $includeBaseDir)

EDIT: The Scripting guy wrote an article where he shows an alternative way of loading the assembly, it's a bit cleaner looking.

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.