![](https://powershell.work/wp-content/themes/cocoon-master/images/b-woman.png)
Power Shellで
・ファイル/フォルダをZIP圧縮
できます!
※ここで紹介するのは「パスワード無しZIPファイル」の作成(圧縮)方法です。
コード
ここでは例として
・デスクトップ配下の約1GBのフォルダ「test」を圧縮し
・デスクトップ配下にファイル「test.zip」として作成
します。
![フォルダ「test」](https://powershell.work/wp-content/uploads/2022/07/ps_zipfile_compress-archive_001.png)
![フォルダ「test」のサイズは約1GB](https://powershell.work/wp-content/uploads/2022/07/ps_zipfile_compress-archive_002.png)
# ZIP形式で圧縮するフォルダ(またはファイル)パスを指定
$targetPath = "C:\Users\user\Desktop\test"
# 作成するZIPファイルのパスを指定
$zipFilePath = "C:\Users\user\Desktop\test.zip"
$ws = New-Object -ComObject Wscript.Shell
try
{
Compress-Archive -Path $targetPath -DestinationPath $zipFilePath -Force
$ws.popup("圧縮に成功しました。")
}
catch
{
$ws.popup("エラー : " + $PSItem)
}
実行結果
フォルダをZIP形式で圧縮できました。
![実行結果①](https://powershell.work/wp-content/uploads/2022/07/ps_zipfile_compress-archive_003.png)
![実行結果②](https://powershell.work/wp-content/uploads/2022/07/ps_zipfile_compress-archive_004.png)
サイズが圧縮されていることも確認できました。
※「約1GB」から「約0.2GB(=約200MB)」へ圧縮されました。
![実行結果③](https://powershell.work/wp-content/uploads/2022/07/ps_zipfile_compress-archive_005.png)
参考①
ZIP形式のファイルを解凍(展開)することもできます。
詳細は以下の記事をご確認ください。
参考②
上記で使用した以下の詳細は、公式サイトをご確認ください。
●「Compress-Archive」コマンドレット