PowerShellでファイルをZIP圧縮したり展開(解凍)する方法

PowerShellでファイルをZIP圧縮したり展開(解凍)する方法

Windows 10などに標準搭載されているPowerShellでは、バージョン5.0でファイルをZIP形式で圧縮したり展開(解凍)する、以下のコマンドレットが追加されています。

  • ファイルをZIP圧縮するコマンドレット「Compress-Archive」
  • ZIPファイルを展開するコマンドレット「Expand-Archive」

そこでここでは、Windows 10のPowerShellで上で紹介したコマンドレットを使って、ファイルをZIP形式で圧縮したり展開(解凍)する方法を紹介します。

Windows 10の標準機能でZIPファイルを作成/展開(解凍)する方法
ここでは、Windows 10の標準機能だけで、ZIPファイルを作成したりZIPファイルを展開(解凍)する方法を紹介します。

動作環境

この記事は、以下の環境で実行した結果を基にしています。他のエディションやバージョンでは、動作結果が異なる場合があることをご了承ください。

ソフトウェアバージョン
Windows 10 Pro 64bit1909
Windows PowerShell5.1.18362.628

ご自分の環境のWindows PowerShellのバージョンは「Get-Host」コマンドレットで確認できます。

PowerShellでファイルをZIP圧縮したり展開(解凍)する方法

ファイルをZIP圧縮する

ファイルをZIP圧縮するときは「Compress-Archive」コマンドレットを利用します。

Compress-Archive | Microsoft Docs

基本的な書式は、次のとおりです。

PS> Compress-Archive -Path <圧縮したいファイル> -DestinationPath <出力ファイル名>

「-Path」オプションで圧縮するファイルやフォルダーを指定し「-DestinationPath」オプションで出力するファイル名を指定します。

単一ファイルを圧縮する

単一のファイルを圧縮する場合は、次のように実行します。

PS> Compress-Archive -Path E:sample.txt -DestinationPath E:test.zip

複数ファイルを圧縮する

複数のファイルを圧縮する場合は、カンマ区切りで複数ファイルを指定するか、ワイルドカードで指定することができます。

PS> Compress-Archive -Path E:sample.txt, E:samaple2.txt -DestinationPath E:test2.zip
PS> Compress-Archive -Path E:*.txt -DestinationPath test3.zip

ワイルドカードを使用したくないとき

意図しないファイルが対象とならないよう、意識的にワイルドカードを使用しない場合は、「-Path」オプションの代わりに「-LiteralPath」オプションを利用します。

PS> Compress-Archive -LiteralPath E:sample.txt, E:samaple2.txt -DestinationPath E:test2.zip

圧縮するファイルをパイプで渡す

圧縮するファイルは、パイプで渡すこともできます。

PS> Get-ChildItem E:*.txt | Compress-Archive -DestinationPath test4.zip

既存のZIPファイルにファイルを追加する

「-Update」オプションを利用すると、既存のZIPファイルにファイルを追加できます。

PS> Compress-Archive -Path E:sample3.txt -DestinationPath E:test3.zip -Update

圧縮レベルを指定する

「-CompressionLevel」オプションを利用すると、圧縮率や圧縮スピードを指定できます。

指定可能な値は、次のとおりです。

  • Optimal - 最適な圧縮率を使用します。(デフォルト)
  • Fastest - 処理時間が最速となる圧縮率を使用します。
  • NoCompression - 圧縮は行わず、書庫化だけを行います。
PS> Compress-Archive -Path E:*.txt -DestinationPath test3.zip -CompressionLevel Fastest

コマンドレットのヘルプ

「Compress-Archive」コマンドレットのヘルプは、次のとおりです。

PS> Get-Help Compress-Archive

名前
    Compress-Archive

概要
    Creates an archive, or zipped file, from specified files and folders.


構文
    Compress-Archive [-Path] <String[]> [-DestinationPath]  [-CompressionLevel {Optimal | NoCompression | Faste
    st}] [-Confirm] -Force [-WhatIf] []

    Compress-Archive [-DestinationPath]  [-CompressionLevel {Optimal | NoCompression | Fastest}] [-Confirm] -Fo
    rce -LiteralPath <String[]> [-WhatIf] []

    Compress-Archive [-DestinationPath]  [-CompressionLevel {Optimal | NoCompression | Fastest}] [-Confirm] -Li
    teralPath <String[]> -Update [-WhatIf] []

    Compress-Archive [-DestinationPath]  [-CompressionLevel {Optimal | NoCompression | Fastest}] [-Confirm] -Li
    teralPath <String[]> [-WhatIf] []

    Compress-Archive [-Path] <String[]> [-DestinationPath]  [-CompressionLevel {Optimal | NoCompression | Faste
    st}] [-Confirm] [-WhatIf] []

    Compress-Archive [-Path] <String[]> [-DestinationPath]  [-CompressionLevel {Optimal | NoCompression | Faste
    st}] [-Confirm] -Update [-WhatIf] []


説明
    The Compress-Archive cmdlet creates a zipped (or compressed) archive file from one or more specified files or folde
    rs. An archive file allows multiple files to be packaged, and optionally compressed, into a single zipped file for
    easier distribution and storage. An archive file can be compressed by using the compression algorithm specified by
    the CompressionLevel parameter.

    Because Compress-Archive relies upon the Microsoft .NET Framework API System.IO.Compression.ZipArchive to compress
    files, the maximum file size that you can compress by using Compress-Archive is currently 2 GB. This is a limitatio
    n of the underlying API.


関連するリンク
    Online Version: http://go.microsoft.com/fwlink/?LinkId=821654

注釈
    例を参照するには、次のように入力してください: "get-help Compress-Archive -examples".
    詳細を参照するには、次のように入力してください: "get-help Compress-Archive -detailed".
    技術情報を参照するには、次のように入力してください: "get-help Compress-Archive -full".
    オンライン ヘルプを参照するには、次のように入力してください: "get-help Compress-Archive -online"


PS C:>

ZIPファイルを展開(解凍)する

ZIPファイルを展開(解凍)するときは「Expand-Archive」コマンドレットを利用します。

Expand-Archive | Microsoft Docs

基本的な書式は、次のとおりです。

PS> Expand-Archive -Path <ZIPファイル> -DestinationPath <展開先フォルダー>

「-Path」オプションでZIPファイルを指定し「-DestinationPath」オプションで展開先のフォルダーを指定します。

PS> Expand-Archive -Path E:test.zip -DestinationPath E:test

「-DestinationPath」オプションを指定せずに実行すると、カレントディレクトリにZIPファイル名と同名のフォルダーが作成され、その中に解凍されます。

以下の例では、Cドライブ直下に「test」という名前のフォルダーが作成され、その中に解凍されます。

PS> Expand-Archive -Path E:Test.zip

展開先のフォルダーに同名のファイルがある場合、そのまま展開するとエラーとなるため、上書きする場合は「-Force」オプションを使用します。

PS> Expand-Archive -Path E:Test.zip -DestinationPath E:test -Force

コマンドヘルプ

「Expand-Archive」コマンドレットのヘルプは、次のとおりです。

PS> Get-Help Expand-Archive

名前
    Expand-Archive

概要
    Extracts files from a specified archive (zipped) file.


構文
    Expand-Archive [[-DestinationPath] ] [-Confirm] [-Force] -LiteralPath  [-WhatIf] []

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


説明
    The Expand-Archive cmdlet extracts files from a specified zipped archive file to a specified destination folder. An
     archive file allows multiple files to be packaged, and optionally compressed, into a single zipped file for easier
     distribution and storage.


関連するリンク
    Online Version: http://go.microsoft.com/fwlink/?LinkId=821655

注釈
    例を参照するには、次のように入力してください: "get-help Expand-Archive -examples".
    詳細を参照するには、次のように入力してください: "get-help Expand-Archive -detailed".
    技術情報を参照するには、次のように入力してください: "get-help Expand-Archive -full".
    オンライン ヘルプを参照するには、次のように入力してください: "get-help Expand-Archive -online"


PS C:>

あとがき

Windows 10で、サードパーティーの圧縮・解凍ツールを利用したい場合は、以下の記事が参考になるでしょう。

Windows向けの無料圧縮解凍ソフトの定番「Lhaplus」
ここでは、Windows向けで個人/企業を問わず無料で利用できるおすすめの圧縮解凍ソフト「Lhaplus(ラプラス)」の特徴と使い方を紹介します。
Windows向け無料&多機能な圧縮解凍ツール「PeaZip」
ここでは、Windows向けの無料の圧縮解凍ツールの中でも、ファイルの破損チェックやハッシュ値計算、形式変換、コマンド操作などの多機能さでおすすめの「PeaZip」を紹介します。