Wednesday, October 22, 2014

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: ,,,

Monday, October 20, 2014

Another PowerCamp is Over

Last weekend, I held another PowerShell PowerCamp event – we had 19 attendees in all for two days of PowerShell. It was a great weekend, topped off by Tom Arbuthnot coming in on the Sunday to deliver a guest lecture.  I really enjoyed it, but it was a lot of work! And from the mail I've had, the attendees got a lot out of it.

Thanks to the folks at Microsoft, particularly Claire Smyth, for all their help in making the weekend a success.

During the event, there were several ideas put forward to a next event. One suggestion was a more advanced weekend, particularly looking at DSC and the new things in Version 5. A V5 PowerCamp could be fun! Ideas as to future events are most welcome.

Technorati Tags: ,

PowerShell Version 5 in Windows 10 Preview

Like a lot of people, I’ve downloaded and been playing with the previews of Windows 10 (client and server). About the first thing I did was to check out PowerShell and was delighted to find it contains a very late drop of the PowerShell V5 code – marginally later than the WMF Preview shipped in September.

My Server 2012R2 workstation, which has the September V5 drop reports:

image

Meanwhile, in the Windows 10 Server preview, we see:

image

As you can see, Windows 10 has a slightly newer version of PowerShell V5. The release notes don’t clarify what has changed, but I’d have expected the 9841 to be just a bug fixed fork of 9814.

This new version of PowerShell brings some big changes, not least of which is the improvements and enhancements to Desired State Configuration – no doubt this will feature large in the eventual launch of Windows 10 Server (Server 2015??).

But there are also a bunch of smaller changes that I also am very keen to see go mainstream. These include:

  • Archive (.ZIP file) cmdlets
  • Changes to –item noun to enable symbolic links
  • The ability to develop classes (and enums) using PowerShell
  • Improvements in DSC authoring within the ISE
  • PowerShell transcripts within the ISE
  • Better script tracing

PowerShell V5 looks like being a block buster! I’ll be writing blog articles about each of these new features along with some usage of them for your delight!

Technorati Tags: