1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PowerShellを活用してZIPファイルを一括解凍する

Last updated at Posted at 2024-06-19

開発者やシステム管理者の日常業務において、複数のZIPファイルを効率的に解凍し、整理する必要がしばしばあります。PowerShellのスクリプトを使用することで、このような作業を自動化し、手作業による時間の浪費やエラーを削減することが可能です。今回は、特定のディレクトリ内のすべてのZIPファイルを自動で解凍し、それぞれのアーカイブ内容を同名の新しいフォルダに展開するスクリプトについて解説します。

スクリプトの説明

以下のスクリプトは、カレントディレクトリ内の全てのZIPファイルを対象に実行されます。

Get-ChildItem *.zip | ForEach-Object { $destPath = Join-Path $.DirectoryName $.BaseName; New-Item -ItemType Directory -Force -Path $destPath; Expand-Archive -Path $_.FullName -DestinationPath $destPath -Force }
1. Get-ChildItem *.zip

このコマンドは、カレントディレクトリから.zip拡張子を持つファイルをすべて取得します。Get-ChildItem は、ファイルやディレクトリの検索に使用されるコマンドです。

2. ForEach-Object

取得したZIPファイルそれぞれに対して、後続のコマンドブロックを実行します。$_ は、現在処理中のオブジェクト(ここではZIPファイル)を指します。

3. `Join-Path $.DirectoryName $.Base

Name`
ZIPファイルが存在するディレクトリと、ファイル名(拡張子を除く)を結合して、新しいディレクトリのパスを生成します。これが解凍先のフォルダパスになります。

4. New-Item -ItemType Directory -Force -Path $destPath

解凍先のディレクトリを作成します。-Force オプションは、指定したパスにディレクトリが既に存在してもエラーを出さずに処理を進めるために使用します。

5. Expand-Archive -Path $_.FullName -DestinationPath $destPath -Force

Expand-Archive コマンドを用いて、ZIPファイルを解凍します。-Path $_.FullName でZIPファイルのフルパスを指定し、-DestinationPath $destPath で解凍先を指定します。こちらも -Force オプションにより、解凍先のパスにファイルが存在しても上書きします。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?