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:
- Open PowerShell:
- Press
Windows + X
and select PowerShell or Windows Terminal.
- Press
- 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.
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:
- 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.
- Replace
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 withExpand-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.