LoginSignup
55
64

More than 5 years have passed since last update.

超簡単! Powershellでファイルの圧縮/解凍

Last updated at Posted at 2019-03-03

powershellを使ったファイルの圧縮/解凍

powershellで簡単にファイルを圧縮/解凍できたため、メモ。

1. 圧縮

Compress-Archive

Compress-Archive -Path C:\Users\.... -DestinationPath C:\Users\XXX\....

これだけ。めちゃめちゃ簡単。
以下、パラメータの解説。

-Path
必須パラメータ。Compress(圧縮)するファイル/フォルダのパスを指定する。
複数ファイル指定することも可能。ワイルドカードの使用も可能。

-DestinationPath
必須パラメータ。Compress(圧縮)先のパスを指定する。

-Force
上書きする。-Forceパラメータ無しでは、-DestinationPathで指定したzipファイルが既に存在する場合、実行に失敗する。このパラメータを知らず、先日ジョブがこけた。

-Update
既存のzipファイルを更新する。

その他、CompressionLevel、Confirm等のパラメータがあるっぽい。
詳しく書いてある記事がこちら。
[https://cheshire-wara.com/powershell/ps-help/compress-archive-help/]

2. 解凍

Expand-Archive

Expand-Archive -Path C:\Users\.... -DestinationPath C:\Users\XXX\....

これだけ。これもめちゃめちゃ簡単。
以下、パラメータの解説。

-Path
必須パラメータ。Expand(展開)するzipファイルを指定する。

-DestinationPath
必須パラメータ。Expand(展開)先のパスを指定する。

-Force
上書きする。

作ってみた

1 zipファイルを作成するバッチファイルを作成した。

compress-archive.bat
@echo off

rem -フォルダの有無を確認し、ファイルが存在した場合圧縮する

set base_path=C:\Users\XXX\Desktop\compress
set powershell_exe="C:\Users\XXX\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell\Windows PowerShell.lnk"
set destination_path=C:\Users\XXX\Desktop\destination

if not exist %base_path% (
    eventcreate /T ERROR /SO test /ID 001 /D "test用イベントログ" /L APPLICATION
    exit /b 1
)

%powershell_exe% Compress-Archive -Path %base_path%\* -DestinationPath %destination_path%\test.zip -Force

exit /b 0

2 zipファイルを解凍するバッチファイルを作成した。

@echo off

rem -ファイルの有無を確認し、あった場合解凍する

set zip_file=C:\Users\XXX\Desktop\destination\test.zip
set powershell_exe="C:\Users\XXX\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell\Windows PowerShell.lnk"
set destination_path=C:\Users\XXX\Desktop\expand

if not exist %zip_file% (
    eventcreate /T ERROR /SO test /ID 002 /D "test用イベントログ" /L APPLICATION
    exit /b 1
)

%powershell_exe% Expand-Archive -Path %zip_file% -DestinationPath %destination_path% -Force

exit /b 0

55
64
1

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
55
64