Compressing and uncompressing files with PowerShell

If you wish to create a zip file containing the contents of a folder on a Microsoft Windows system from a command line interface (CLI), you can use PowerShell for that purpose. If you have version 5.0 or later of PowerShell, you can use the compress-archive and expand-archive cmdlets - cmdlets (pronounced command-lets) are specialized .NET classes implementing a particular operation. You can determine which version of PowerShell you have by opening a PowerShell window and typing $psversiontable. E.g., in the example below, the PowerShell version is 3.0, so the cmdlets are not present:

PS C:\Users\John\Documents> $psversiontable

Name                           Value
----                           -----
PSVersion                      3.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.34209
BuildVersion                   6.2.9200.17065
PSCompatibleVersions           {1.0, 2.0, 3.0}
PSRemotingProtocolVersion      2.2


PS C:\Users\John\Documents> compress-archive -Path .\Anniversary_Update -DestinationPath .\Anniversary_Update.zip
compress-archive : The term 'compress-archive' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ compress-archive -Path .\Anniversary_Update -DestinationPath .\Anniversary_Updat ...
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (compress-archive:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users\John\Documents>

However, the cmdlets will work if version 5.0 or later is present as shown below:

Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS C:\Users\Liza\documents> $psversiontable

Name                           Value
----                           -----
PSVersion                      5.1.14393.187
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.187
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


PS C:\Users\Liza\documents> compress-archive -Path ./Temp -DestinationPath ./temp.zip
PS C:\Users\Liza\documents> expand-archive -Path temp.zip -DestinationPath c:\users\liza\documents\example
PS C:\Users\Liza\documents>

In the first example above the contents of the Temp directory which is a directory beneath the directory from which the command is being run will be placed in the file temp.zip, which will be created in the directory from which the command is being run. In the second example, the files within the just created zip file will be extrated to a directory named example, which doesn't currently exist, but will be created by the cmdlet. Within the newly created example directory will be a Temp subdirectory containing the files that were in the Temp directory directly beneath the directory from which the cmdlets were executed.

If you attempt to create a zip file at the same location as one that currently exists with the same name, you will see an error message like the one below:

PS C:\Users\Liza\documents> compress-archive -Path ./SNMP -DestinationPath ./SNMP.zip
compress-archive : The archive file C:\Users\Liza\documents\SNMP.zip already exists. Use the -Update parameter to
update the existing archive file or use the -Force parameter to overwrite the existing archive file.
At line:1 char:1
+ compress-archive -Path ./SNMP -DestinationPath ./SNMP.zip
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (C:\Users\Liza\documents\SNMP.zip:String) [Compress-Archive], IOExcepti
   on
    + FullyQualifiedErrorId : ArchiveFileExists,Compress-Archive

PS C:\Users\Liza\documents>

Category: PowerShell

 

TechRabbit ad 300x250 newegg.com

Justdeals Daily Electronics Deals1x1 px