Computer >> Hướng Dẫn Máy Tính >  >> Hệ Thống >> Windows

Cách nén và giải nén file bằng PowerShell trên Windows 11/10

Định dạng ZIP nén file thành một file duy nhất, tiết kiệm dung lượng, mã hóa dữ liệu, dễ chia sẻ. Bài viết hướng dẫn nén (zip) và giải nén (unzip) file bằng PowerShell trên Windows 11/10.

Nén file bằng PowerShell

Dùng cmdlet Compress-Archive. Cú pháp:

Compress-Archive -LiteralPath <ĐườngDẫnFile> -DestinationPath <ĐườngDẫnĐích>

Mở PowerShell: Win + X > I.

Ví dụ nén nhiều file (cách nhau bằng dấu phẩy):

Compress-Archive -LiteralPath "C:\File1.txt","C:\File2.jpg" -DestinationPath "C:\Archive.zip"

Cách nén và giải nén file bằng PowerShell trên Windows 11/10

Lưu ý: Phải đặt tên file .zip ở đích, không thì PowerShell lưu thành ".zip". Dùng ngoặc kép nếu đường dẫn có dấu cách.

Nén cả thư mục (kèm thư mục con):

Compress-Archive -LiteralPath "C:\MyFolder" -DestinationPath "C:\MyFolder.zip"

Cách nén và giải nén file bằng PowerShell trên Windows 11/10

Sử dụng ký tự đại diện (*)

Dùng tham số -Path (thay -LiteralPath) để hỗ trợ wildcard.

  • Loại bỏ thư mục gốc khỏi file ZIP: thêm * cuối đường dẫn.
    Compress-Archive -Path "C:\path\to\file\*" -DestinationPath "C:\path\to\archive.zip"
  • Chỉ nén file loại cụ thể (ví dụ .docx):
    Compress-Archive -Path "C:\path\to\file\*.docx" -DestinationPath "C:\path\to\archive.zip"
  • Nén tất cả file trong thư mục gốc và thư mục con (không bao gồm thư mục con):
    Compress-Archive -Path "C:\path\to\file\*.*" -DestinationPath "C:\path\to\archive.zip"

Cách nén và giải nén file bằng PowerShell trên Windows 11/10

Cách nén và giải nén file bằng PowerShell trên Windows 11/10

Cách nén và giải nén file bằng PowerShell trên Windows 11/10

Cập nhật file ZIP đã có dùng tham số -Update:

Compress-Archive -Path "C:\path\to\files" -Update -DestinationPath "C:\path\to\archive.zip"

Cách nén và giải nén file bằng PowerShell trên Windows 11/10

Đọc thêm: Mở file .TAR.GZ, .TGZ, .GZ.

Giải nén file bằng PowerShell

Dùng cmdlet Expand-Archive. Cú pháp:

Expand-Archive -LiteralPath <ĐườngDẫnFileZip> -DestinationPath 

Mở PowerShell, chạy lệnh. Thư mục đích sẽ chứa nội dung giải nén. Nếu chưa tồn tại, PowerShell tự tạo.

Cách nén và giải nén file bằng PowerShell trên Windows 11/10

Mặc định (không có -DestinationPath): giải nén vào thư mục hiện tại, tạo thư mục mới tên trùng file ZIP.

Ví dụ chỉ định thư mục Docs:

Expand-Archive -LiteralPath "C:\Archive.zip" -DestinationPath "C:\Users\Name\Docs"

Cách nén và giải nén file bằng PowerShell trên Windows 11/10

Nếu thư mục đích đã tồn tại, PowerShell báo lỗi. Dùng tham số -Force để ghi đè (cẩn thận: dữ liệu cũ mất vĩnh viễn).

Tóm lại: PowerShell cung cấp Compress-Archive và Expand-Archive mạnh mẽ để nén/giải nén ZIP trên Windows 11/10!

Cách nén và giải nén file bằng PowerShell trên Windows 11/10