Managing Zip Files with PowerShell V5

In a recent blog post, I noted the arrival of PowerShell V5 preview within the technical preview of Windows 10. A slightly later version than is available for installation on down-level operating systems ( I use the latest V5 preview version both on my Windows 8.1 laptop and my main home workstation (which runs Server 2012 R2).  While Desired State Configuration is the ‘big’ new feature in V5, there are a lot of other cool features in V5, including the ability to manage zip files with PowerShell.

The later V5 preview versions come with two new cmdlets or managing  ZIP files:

  • Compress-Archive
  • Expand-Archive

The Compress-Archive cmdlet takes an array of file names (specified via the –Path parameter) and compresses them into an archive file (specified by the -DestinationPath Parameter). You can also specify the level of compression – the valid values are NoCompression, Optimal, Fastest. Fastest means that the compression is less than  Optimal, but Optimal should produce slightly smaller .ZIP files. Optimal, however, takes more CPU time – but there’s not much difference for smaller files. To have Compress-Archive update some or all the files in the archive, use the –Update parameter. This parameter updates the archive with the specified files. Thus you can update a single file in an archive should the need arise.

The Expand-Archive cmdlet expands the files in the archive (specified by the –Path parameter) into the folder specified by the –DestinationPath parameter. To avoid being asked for permission to do the expansion, you can speciy the –Force parameter.

 

Here’s an example of some archive file manipulation

 

PSH [C:\foo]: ls c:\foo\*.txt

    Directory: C:\foo
Mode                LastWriteTime         Length Name                                         
----                -------------         ------ ----                                         
-a----       26/07/2014     15:59            431 atia.txt                                     
-a----       19/06/2014     17:25          18890 history.txt                                  
-a----       06/03/2014     15:09           1027 hosts.txt                                    
-a----       06/08/2014     10:05             37 NAMES.TXT                                    
-a----       24/09/2014     06:58           1877 trans1.txt                                   

PSH [C:\foo]: (ls *.txt | measure -sum -Property length).sum
22262

PSH [C:\foo]: Compress-Archive *.txt -DestinationPath .\aaa1.zip -CompressionLevel Fastest

PSH [C:\foo]: Compress-Archive *.txt -DestinationPath .\aaa2.zip -CompressionLevel Optimal

PSH [C:\foo]: Compress-Archive *.txt -DestinationPath .\aaa3.zip -CompressionLevel NoCompression

PSH [C:\foo]: ls c:\foo\aaa?.zip

    Directory: C:\foo

Mode                LastWriteTime         Length Name                                         
----                -------------         ------ ----                                         
-a----       21/10/2014     18:33           2172 aaa1.zip                                     
-a----       21/10/2014     18:33           2039 aaa2.zip                                     
-a----       21/10/2014     18:33          22783 aaa3.zip                                     

PSH [C:\foo]: Expand-Archive c:\foo\aaa1.zip -DestinationPath c:\foo\expanded -Force

PSH [C:\foo]: dir .\expanded

    Directory: C:\foo\expanded

Mode                LastWriteTime         Length Name                                         
----                -------------         ------ ----                                         
-a----       21/10/2014     18:34            431 atia.txt                                     
-a----       21/10/2014     18:34          18890 history.txt                                  
-a----       21/10/2014     18:34           1027 hosts.txt                                    
-a----       21/10/2014     18:34             37 NAMES.TXT                                    
-a----       21/10/2014     18:34           1877 trans1.txt
                                   

In summary, two new and somewhat overdue cmdlets that make your life easier!

Technorati Tags: ,,,

0 Comments