Monday, January 6, 2025
HomeComputer ScienceHow to Unzip a File in PowerShell

How to Unzip a File in PowerShell

PowerShell provides built-in cmdlets for managing compressed files. Here’s how you can unzip a file using PowerShell:

Method 1: Using the Expand-Archive Cmdlet

The Expand-Archive cmdlet is the easiest way to unzip a .zip file in PowerShell.

Steps:

  1. Open PowerShell:
    • Press Windows + X and select PowerShell or Windows Terminal.
  2. Run the Command:
    Expand-Archive -Path "C:\Path\To\File.zip" -DestinationPath "C:\Path\To\Extract"
    
    • -Path: Specifies the location of the .zip file.
    • -DestinationPath: Specifies where the extracted files will be stored.
See also  What Is Computer Science Salary?

Example:

To extract example.zip to C:\ExtractedFiles:

Expand-Archive -Path "C:\example.zip" -DestinationPath "C:\ExtractedFiles"

Method 2: Using .NET System.IO.Compression Class

If you are using an older version of PowerShell (before version 5.0), you can use .NET libraries.

Steps:

  1. Run the Script:
    Add-Type -AssemblyName System.IO.Compression.FileSystem
    [System.IO.Compression.ZipFile]::ExtractToDirectory("C:\Path\To\File.zip", "C:\Path\To\Extract")
    
    • Replace "C:\Path\To\File.zip" with the path to your .zip file.
    • Replace "C:\Path\To\Extract" with the destination folder.

Example:

To extract backup.zip to D:\Data:

Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory("C:\backup.zip", "D:\Data")

Method 3: Using Compress-Archive to Verify Compression

To verify that you can both compress and decompress in PowerShell, you can create a zip file and extract it:

  • Compress:
    Compress-Archive -Path "C:\MyFolder" -DestinationPath "C:\MyFolder.zip"
    
  • Decompress:
    Expand-Archive -Path "C:\MyFolder.zip" -DestinationPath "C:\MyFolderUnzipped"
    

Tips:

  • Ensure the Destination Path exists before running the command. If it doesn’t, PowerShell will create the folder.
  • Use the -Force parameter with Expand-Archive to overwrite files in the destination folder:
    Expand-Archive -Path "C:\file.zip" -DestinationPath "C:\folder" -Force
    

These methods are simple and work across most PowerShell environments.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Who is Mike Tyson?

Who is Chase Hudson?

Who is Nikocado Avocado?

Who is iCarly?

Recent Comments

0
Would love your thoughts, please comment.x
()
x