How to Create ZIP Archives and Unzip Files with PowerShell?


You can use PowerShell’s features to create ZIP archives and extract them. In PowerShell 5.0 (this PowerShell version is installed by default on Windows 10), a separate module Microsoft.PowerShell.Archive is available. On older versions of Windows, you can use the ZipFile class from the .NET Framework for archiving.

There are only two cmdlets in the Microsoft.PowerShell.Archive module (C:WindowsSystem32WindowsPowerShellv1.0ModulesMicrosoft.PowerShell.Archive):

  • Compress-Archive
  • Expand-Archive

Get-Command -Module Microsoft.PowerShell.Archive | Format-Table -AutoSize

CommandType Name             Version Source
----------- ----             ------- ------
Function    Compress-Archive 1.0.1.0 Microsoft.PowerShell.Archive
Function    Expand-Archive   1.0.1.0 Microsoft.PowerShell.Archive

Microsoft.PowerShell.Archive module

Let’s look at examples of using these cmdlets to create and extract ZIP archives in your PowerShell scripts.

PowerShell: Create ZIP Archives with Compress-Archive

The Compress-Archive command has the following syntax:

Compress-Archive [-Path] String[] [-DestinationPath] String  [-CompressionLevel String ] [-Update]

  • The Path parameter is used to specify the path to the files or folders to be archived;
  • DestinationPath – specifies the path to the ZIP file;
  • CompressionLevel – sets the compression level (NoCompression, Optimal or Fastest);
  • Update – allows you to add (update) files in an existing ZIP archive;
  • Force – if an archive with the specified name already exists, it will be overwritten.
Tip. Compression level options:

  • Optimal — optimization by the level of compression;
  • Fastest — optimization by the time taken;
  • NoCompression — without any compression.

The NoCompression option should be used when archiving already compressed files (jpg, msi, mp3, etc.) into a single ZIP file. In this case, Windows will not waste CPU time compressing them.

To zip a single file, run:

Compress-Archive -Path "C:LogsWindowsUpdate.log" -DestinationPath C:Archiveupdatelog.zip -CompressionLevel Optimal

How to Zip/Unzip files or folders using PowerShell?

You can zip the entire contents of multiple folders (including all files and nested folders):

Compress-Archive -Path C:Logs,C:Logs2 -DestinationPath C:Archivelogs-all.zip -CompressionLevel Optimal

To add multiple files or folders to the archive, separate their names with commas.

You can add to ZIP archive only files with a specific mask. For example, the following command will zip only *.txt files.

Compress-Archive -Path C:Logs*.txt -DestinationPath C:Archivelogs-txt.zip –CompressionLevel Fastest

More complex filters can be used with the Get-ChildItem cmdlet. For example, the following script will allow you to find the top 10 largest files with the *.docx or *.xlsx extensions on the disk and add them to the archive:

Get-ChildItem c:shareITdept -Include *.xlsx –Recurse| sort -descending -property length | select -first 10 |Compress-Archive -DestinationPath C:backupitdeptdocs.zip

To add new files to an existing zip archive, use the Update key:

Compress-Archive -Path C:Logs,C:logs2 –Update -DestinationPath C:Archivelogs-txt.zip

Hint.Because the Microsoft.PowerShell.Archive module uses the System.IO.Compression.ZipArchive class, you cannot compress a file larger than 2 GB (because there’s a limitation of the underlying API). When trying to compress a larger file, an error will appear:

Compress-Archive - Stream was too long error

Exception calling "Write" with "3" argument(s): "Stream was too long."
At  C:Windowssystem32WindowsPowerShellv1.0ModulesMicrosoft.PowerShell.ArchiveMicrosoft.PowerShell.Archive.psm1:805
char:29
+ ...                     $destStream.Write($buffer, 0, $numberOfBytesRead)
+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : IOException

How to Extract a ZIP File with Expand-Archive?

You can use the Expand-Archive cmdlet to unzip ZIP files. The syntax for the cmdlet is similar:

Expand-Archive [-Path] String [-DestinationPath] String [-Force]  [-Confirm]

For example, to unpack the ZIP archive we created earlier into the specified folder and overwrite the files:

Expand-Archive -Path C:archivelogs-all.zip  -DestinationPath c:logs -Force

Expand-Archive powershell cmdlet

Disadvantages of the Microsoft.PowerShell.Archive module:

  • You cannot view the contents of the archive without extracting it;
  • You cannot extract some of the files from the archive (you will have to extract the entire archive file);
  • You cannot use other archive formats, except zip;
  • You won’t be able to protect a ZIP archive with a password.
In more complex cases, you need to use third-party tools to perform archiving in your PowerShell scripts. For example, 7zip or 7Zip4Powershell module.

You can install the 7Zip4Powershell module and extract a password protected zip file as follows:

Install-Module -Name 7Zip4Powershell
Expand-7Zip -ArchiveFileName C:ArchiveLogs.zip -Password "p@ssd0rw" -TargetPath C:ShareLogs

Working with Compressed Files with the PowerShell ZipFile Class

In earlier Windows version (prior to Windows 10 or Windows Server 2016 with PowerShell version < 5.0 (if you cannot upgrade the PowerShell version), you can use a separate ZipFile class (from NET Framework 4.5) to create zip archives.

First, load the class into your PowerShell session:

Add-Type -AssemblyName "System.IO.Compression.FileSystem"

To archive a folder, use a PS script like this:

$SourceFolder="C:Logs"
$ZipFileName="C:PSlogs.zip"
[IO.Compression.ZipFile]::CreateFromDirectory($SourceFolder, $ZipFileName)

[IO.Compression.ZipFile]::CreateFromDirectory .net class to create zip archive

In order to update the ZIP archive and set the compression ratio, use the following PowerShell code:

$addfile = ‘C:tempnew.log’
$compressionLevel = [System.IO.Compression.CompressionLevel]::Fastest
$zip = [System.IO.Compression.ZipFile]::Open($zipFileName, 'update')[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $addfile, (Split-Path $addfile -Leaf), $compressionLevel)
$zip.Dispose()

The $zip.Dispose() command is used to close the zip file.

You can list the contents of the ZIP archive:

[System.IO.Compression.ZipFile]::OpenRead($zipFileName).Entries.Name

How to view contents of ZIP archive with powershell?

Or you can display the contents of the zip archive as an Out-GridView table with additional info (size of compressed/uncompressed files, last write time, etc.):

$ZipFileName = "C:PSlogs1.zip"
$Stream = New-Object IO.FileStream($ZipFileName , [IO.FileMode]::Open)
$ZipArchive = New-Object IO.Compression.ZipArchive($Stream)
$ZipArchive.Entries |
Select-Object Name,
@{Name="File Path";Expression={$_.FullName}},
@{Name="Compressed Size (KB)";Expression={"{0:N2}" -f($_.CompressedLength/1kb)}},
@{Name="UnCompressed Size (KB)";Expression={"{0:N2}" -f($_.Length/1kb)}},
@{Name="File Date";Expression={$_.LastWriteTime}} | Out-GridView
$ZipArchive.Dispose()
$Stream.Close()
$Stream.Dispose()

file properties in zip archive: compressed/uncompressed size, last write time

To extract the ZIP file to the C:Logs folder, use the following commands:

$SourceZipFile="C:PSlogs.zip"
$TargetFolder="C:Logs"
[IO.Compression.ZipFile]::ExtractToDirectory($SourceZipFile, $TargetFolder)





Source link

Mourad ELGORMA

Mourad ELGORMA

Fondateur de summarynetworks, passionné des nouvelles technologies et des métiers de Réseautique , Master en réseaux et système de télécommunications. ,j’ai affaire à Pascal, Delphi, Java, MATLAB, php …Connaissance du protocole TCP / IP, des applications Ethernet, des WLAN …Planification, installation et dépannage de problèmes de réseau informatique……Installez, configurez et dépannez les périphériques Cisco IOS. Surveillez les performances du réseau et isolez les défaillances du réseau. VLANs, protocoles de routage (RIPv2, EIGRP, OSPF.)…..Manipuler des systèmes embarqués (matériel et logiciel ex: Beaglebone Black)…Linux (Ubuntu, kali, serveur Mandriva Fedora, …). Microsoft (Windows, Windows Server 2003). ……Paquet tracer, GNS3, VMware Workstation, Virtual Box, Filezilla (client / serveur), EasyPhp, serveur Wamp,Le système de gestion WORDPRESS………Installation des caméras de surveillance ( technologie hikvision DVR………..). ,

Laisser un commentaire