New Feature in PowerShell 5: Compress Files
Summary: Ed Wilson, Microsoft Scripting Guy, talks about the compressing files feature in Windows PowerShell 5.0.
Microsoft Scripting Guy, Ed Wilson, is here. This morning, the sun is shining, the air is dead calm, and I am sitting outside under a gigantic old oak tree with my Windows Surface Pro 3 that I upgraded to Windows 10. I am sipping a cup of English Breakfast tea with a bit of lemon grass, crushed mint leaves, and cinnamon stick while looking over Twitter.
I see lots of people tweeting about Windows PowerShell 5.0. That is a good thing because there are lots of good things in Windows PowerShell 5.0. One of the new features is the Microsoft.PowerShell.Archive module. As shown here, it contains only two cmdlets:
PS C:\> gcm -Module *archive*
CommandType Name Version Source
———– —- ——- ——
Function Compress-Archive 188.8.131.52 Mic…
Function Expand-Archive 184.108.40.206 Mic…
The Compress-Archive function
There are two ways to use the Compress-Archive function, although for practical purposes, there is really only one way. Here are the two parameter sets:
PS C:\> (get-help Compress-Archive).syntax
Compress-Archive [-Path] <String> [-DestinationPath] <String> [-CompressionLevel
<String>] [-Update] [-Confirm] [-WhatIf] [<CommonParameters>]
Compress-Archive [-DestinationPath] <String> [-CompressionLevel <String>] [-Update]
-LiteralPath <String> [-Confirm] [-WhatIf] [<CommonParameters>]
The two parameter sets tell me that I can supply an array of paths or literal paths to the cmdlet. I also need to provide the destination path (as a single string). I can specify the compression level (as a string), and I can use Confirm or Whatif. In addition, I have an update switched parameter that I am assuming will permit me to add a file to an existing archive.
In case you don’t know, an archive, in this context, is what we used to call “zip” files, which are compressed files that end up in files with the .zip file extension. It is pretty much the defacto standard, and for years, Windows has had the ability to create and expand such archives. However, we have not had an easy automation interface—until now. Of course, the PowerShell Community Extension Project (PSCX) has had a couple of .zip cmdlets for years, but now it is brought into Windows PowerShell core. Cool.
Create a .zip file
There are only two required parameters for the Compress-Archive function, and they are Path and DestinationPath. This means I can zip a file (compress the file) by specifying a single file and a destination. This command is shown here:
Compress-Archive -Path 'C:\fso\AMoreComplete.txt' -DestinationPath c:\fso\amorecomplete.zip
While the archive is being created, a progress bar appears at the top of the Windows PowerShell console, and it disappears when the archive is complete. I can then use the Get-ChildItem cmdlet to ensure that the arechive is present:
PS C:\> dir C:\fso\amorecomplete.zip
Mode LastWriteTime Length Name
—- ————- —— —-
-a—- 8/7/2015 9:10 AM 341 amorecomplete.zip
I cannot read inside the .zip file by using Get-Content. As shown here, it pretty much returns gibberish:
On the other hand, I can use Explorer to look inside. I type the following command:
The command opens an Explorer window showing my compressed file:
That is all there is to using Windows PowerShell 5.0 to compress files and folders. Join me tomorrow when I will talk about more cool Windows PowerShell 5.0 stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy