
Fortunately, PowerShell provides a comprehensive set of commands that can help you automate this process and make it more efficient. In this guide, we’ll take a closer look at how to use PowerShell to extract files from archives.
Table of contents
- Introduction to PowerShell Unzip
- Understanding the PowerShell unzip command Expand-Archive
- How to unzip a file using PowerShell?
- How to extract all zip files in a folder using PowerShell
- How do I unzip multiple files in PowerShell?
- Using the ExtractToDirectory Method to Unzip
- How do you read the content of a zip file without unzipping it?
- How to extract a password-protected zip file in PowerShell?
- Troubleshooting Common Issues with PowerShell Unzip
- Conclusion
Introduction to PowerShell Unzip
PowerShell is a powerful scripting language that allows you to automate many different tasks, including file extraction and compression. With PowerShell, you can easily extract files from compressed archives, as well as create your own compressed files. PowerShell uses a set of cmdlets (pronounced “command-lets”) to perform these operations. A cmdlet is a lightweight command that performs a specific action. In the case of file extraction and compression, PowerShell provides Expand-Archive and Compress-Archive cmdlets that you can use to automate these tasks.
Understanding the PowerShell unzip command Expand-Archive
PowerShell unzip refers to the process of extracting files and folders from a ZIP archive using PowerShell commands. The Expand-Archive cmdlet is used to extract files from a compressed archive. To use the PowerShell unzip command, you’ll need to know the path and name of the compressed archive you want to extract files from. You’ll also need to specify the path to which you want to extract the files.
The Expand-Archive cmdlet is a built-in PowerShell command that allows you to extract files from a ZIP archive. This cmdlet is part of Microsoft.PowerShell.Archive module. The Expand-Archive cmdlet was introduced in Windows PowerShell 5.1. You don’t need to install any additional software to use PowerShell Unzip. It’s already installed on your Windows computer.
Now that you understand how the PowerShell unzip command, Expand-Archive. Let’s take a look at how to use PowerShell to extract files from a compressed archive. To use the Expand-Archive cmdlet, you need to specify the path to the ZIP file and the destination path where the extracted files will be stored. The syntax for expand-archive in PowerShell is simple:
Expand-Archive [-Path] <string> [-DestinationPath] <string> [-Force] [-WhatIf] [-Confirm] [-PassThru] [-LiteralPath <string>]
#Expand-Archive -LiteralPath PathToZipFile -DestinationPath PathToDestination Expand-Archive -Path "C:\Temp\LogsArchive.zip" -DestinationPath "C:\Temp\Logs"
This command will extract all the files from the existing zipped file located at “C:\Temp\LogsArchive.zip” and place them in the folder located at “C:\Temp\Logs”. You can verify that the files were extracted correctly by navigating to the destination directory and checking that the files are there. Use quotations around the path when the file path contains a space.

If the zip file is located in the current folder, we just need to specify the zip file using LiteralPath or Path parameter, and optionally, the DestinationPath parameter value. If the “DestinationPath” parameter is not provided, it extracts the archive to the current directory.
Expand-Archive -LiteralPath ".\Scripts.zip"
If you want to overwrite destination files during the extraction process, you can use the -Force parameter:
Expand-Archive -Path 'C:\Archives\Example.zip' -DestinationPath 'C:\ExtractedFiles' -Force
By including the -Force parameter, PowerShell will overwrite existing files with the extracted files, if necessary.
If you find yourself needing to extract multiple ZIP files in a directory, you don’t need to do this one by one. Instead, you can use PowerShell! Let’s say you want to extract all the ZIP files present in the directory “C:\Archives”. You will first need to obtain all the ZIP files in the directory and then extract them one by one. The Get-ChildItem cmdlet, and we’ll utilize a foreach loop to iterate through each file.
How do I unzip multiple files in PowerShell? Here is the PowerShell script to extract all zip files in a Folder:
Get-ChildItem "C:\Archives" -Filter *.zip | ForEach { Expand-Archive -Path $_.FullName -DestinationPath "C:\Extracted" -Force
}
The above command uses the wildcard characters to get all zip files and extract them from the folder “C:\Archives” to the “C:\Extracted”.
How do I unzip multiple files in PowerShell?
Instead of all files in a directory, You can supply specific zip files to extract to a specific folder:
#Parameters
$FilesToExtract = @("C:\Archives\Backup-Set1.zip","C:\Archives\Backup-Set2.zip", "C:\Archives\Backup-Set3.zip")
$DestinationDirectory = "C:\Extracted"
# Loop through each file and extract it
ForEach ($ZipFile in $FilesToExtract) { #Frame Folder to Extract $ExtractedFolderName = $ZipFile -replace ".zip$" $ExtractedFolderPath = Split-Path -Path $ExtractedFolderName -Leaf $DestinationPath = Join-Path -Path $DestinationDirectory -ChildPath $ExtractedFolderPath # Create the destination folder if it doesn't exist If (-not (Test-Path -Path $DestinationPath)) { New-Item -Path $DestinationPath -ItemType Directory | Out-Null } #Extract Each Archive to the Destination Folder Expand-Archive -Path $ZipFile -DestinationPath $DestinationPath -Force
}Another method to unzip files using PowerShell is by using the ZipFile.ExtractToDirectory method is available in the .NET framework. This method allows you to extract files from a ZIP archive using the System.IO.Compression.ZipFile class. It is particularly useful for lower versions of .NET, such as Windows PowerShell 5.1.
Syntax of ZipFile.ExtractToDirectory
Add-Type -AssemblyName System.IO.Compression.FileSystem [System.IO.Compression.ZipFile]::ExtractToDirectory(<SourceFile>, <DestinationPath>)
In the syntax, the <SourceFile> parameter specifies the path to the ZIP file, and the <DestinationPath> parameter specifies the folder where the extracted files will be stored.
Examples of Using ZipFile.ExtractToDirectory
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory("C:\Archives\Example.zip", "C:\ExtractedFiles")Overwriting existing files during extraction
To overwrite existing files during the extraction process, you can include a third parameter, true, in the ExtractToDirectory method:
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory("C:\Archives\Example.zip", "C:\ExtractedFiles", $true)By specifying $true, PowerShell will overwrite existing files with the archive’s contents if necessary.
How do you read the content of a zip file without unzipping it?
You can get the contents of the file without extracting it. Here is how:
Add-Type -AssemblyName System.IO.Compression.FileSystem #Parameters $ZipFilePath = "C:\Temp\Archive.zip" $FileName = "Output.txt" #Get Contents of the Zip File $ZipContents = [System.IO.Compression.ZipFile]::OpenRead($ZipFilePath) # Get the specific entry (file) from the zip archive $SpecificFile = $ZipContents.GetEntry($FileName) #Get Contents of the File $StreamReader = [System.IO.StreamReader]::new($SpecificFile.Open()).ReadToEnd() | Write-Host
How do I extract specific files from a zip file? To extract a specific file from the zip file, use this PowerShell script:
Add-Type -AssemblyName System.IO.Compression.FileSystem
#Parameters
$ZipFilePath = "C:\Temp\Archive.zip"
$SpecificFileName = "Output.txt"
$DestinationFilePath = "C:\Temp\Output.txt"
Try { #Get Contents of the Zip File $ZipContents = [System.IO.Compression.ZipFile]::OpenRead($ZipFilePath) # Get the specific entry (file) from the zip archive $SpecificFile = $ZipContents.GetEntry($SpecificFileName) #Get Contents of the File $inputStream = $SpecificFile.Open() $outputStream = [System.IO.File]::Create($DestinationFilePath) # Copy the content from the input stream to the output stream $inputStream.CopyTo($outputStream) $inputStream.Close() $outputStream.Close()
} Finally { $ZipContents.Dispose()
} The native PowerShell methods don’t support extracting password-protected archives. So, You have to use 3rd party modules like 7zip to extract a password-protected zip file in PowerShell. Here are the steps:
Step 1: Install the “7Zip4Powershell” module
Install-Module -Name 7Zip4Powershell
To find a list of all supported commands from the module, use: Get-Command -Module 7Zip4PowerShell.
Use the Expand-7Zip cmdlet to extract the file. Set the ArchiveFileName, TargetPath, and SecurePassword to define your parameters.
Expand-7Zip -ArchiveFileName 'C:\Archives\Backup.zip' -TargetPath 'C:\Files\' -SecurePassword (Read-Host "Enter password" -AsSecureString)
This will prompt for a password for the archive.
Troubleshooting Common Issues with PowerShell Unzip
While PowerShell unzip operations are generally straightforward, you may encounter some common issues. Here are a few troubleshooting tips to help you overcome these challenges:
- Verify that the ZIP file path and the destination folder path are correct and accessible.
- Check if the ZIP file is not corrupted or encrypted with a password that requires additional steps for extraction.
- Ensure you have the necessary permissions to extract files from the ZIP archive and write them in the destination folder.
- Update your PowerShell version and modules to the latest versions to benefit from bug fixes and improvements that may resolve any known issues.
Conclusion
How to extract zip from the command line?
You can use the Expand-Archive cmdlet to extract a ZIP file using PowerShell. Here’s an example of the command you can use: “Expand-Archive -Path 'C:\path\to\file.zip' -DestinationPath 'C:\path\to\extract'“. Replace the paths with the actual paths to your ZIP file and the destination folder where you want to extract the files.
How to extract 7-Zip files from the command line?
To extract 7-Zip files from the command line, you can use the 7z command-line utility provided by 7-Zip. Here’s how you can extract 7-Zip files using the command line: 7z x archive.7z -oC:\Destination
How do I create a zip file in PowerShell?
To create a zip file in PowerShell, you can open it by searching for it in the Start menu and using the Compress-Archive cmdlet. Here’s an example of how to do it:Compress-Archive -Path C:\PathToFiles -DestinationPath 'C:\Archives\MyArchive.zip' -CompressionLevel NoCompression
Can I overwrite existing files when unzipping with PowerShell?
Yes, you can use the -Force parameter with the Expand-Archive command to overwrite existing files when unzipping.
Is it possible to unzip files to the same directory they are located in?
Yes, you can unzip files to the same directory by omitting the -DestinationPath parameter or setting it to the directory of the zip file. For example, To extract the files and subfolders of a zip file to the root directory, use:$ZipFilePath = "C:\Temp\Logs\Logs1.zip"
Expand-Archive -Path $ZipFilePath -DestinationPath (Get-Item $ZipFilePath).DirectoryName -Force
Can I unzip a specific file from a zip archive?
PowerShell’s Expand-Archive cmdlet does not directly support extracting specific files from an archive. As a workaround, you can extract all files to a temporary directory and then move the specific file(s) you need to your desired location.
Can I unzip password-protected ZIP files using PowerShell?
The built-in Expand-Archive cmdlet does not support unzipping password-protected ZIP files. However, you can use third-party modules or libraries that support password-protected ZIP files. For example, you can use the 7Zip4PowerShell module: Expand-7Zip -ArchiveFileName "C:\Archives\protected.zip" -Password "your_password" -TargetPath "C:\ExtractedFiles"
As it’s well known to us all, PowerShell is a powerful Windows built-in command-line shell and scripting environment tool. It can be used to deal with various works on Windows, such as installing Windows updates, PowerShell copy files, PowerShell gets folder size, installing SSH PowerShell, etc.
However, a great many people and even professionals are not familiar with how to do these works using PowerShell. Here’s a true example from the StackOverflow forum. In this post, we mainly focus on PowerShell unzip/zip files.
I have a .zip file and need to unpack its entire content using PowerShell. I’m doing this but it doesn’t seem to work. What’s wrong? The directory C:a is still empty.
What’s the PowerShell Unzip/Zip Command
First of all, it’s necessary what’s cmdlet can be used to unzip/zip in PowerShell. To unzip file PowerShell on Windows 10/11, you can make full use of the PowerShell Expand-Archive command that can unzip or extract all contents of zipped or archived files to the target folder.
If you want to create a compressed archive or zipped file from specified folders/directories, you can use the PowerShell Compress-Archive command. But you need to make sure the file size is no more than 2GB. In addition, both two commands require that the archive file extension is .zip.
Now, you should have an overall understanding of the zip/unzip PowerShell commands. Let’s see how to zip/unzip in PowerShell Windows 10/11.
Make sure your computer has PowerShell v5+ installed. If not, you can refer to this post “How to Update PowerShell Version to v7.2.5 for Windows 10/11” to install the latest version.
How to Unzip File PowerShell on Windows 10/11
It is very simple to unzip a file using the Expand-Archive PowerShell command on Windows 10/11. Here we will 2 examples to make PowerShell extract zipped files.
# Example 1: PowerShell Unzip an Existing Archive File into a Specified Folder
1. Press the Win + R keys to open the Run box, and then type powershell in it and press Ctrl + Shift + Enter keys to open the elevated PowerShell window.
Make sure you replace the file path/name and target path according to your actual situation.


3. If you want to overwrite an existing extracted file, you can add the -Force parameter at the end of the same PowerShell Expand-Archive command.
# Example 2. PowerShell Unzip an Existing Archive File in the Current Folder

Similarly, you can add the -Force parameter at the end of the command to overwrite an existing extracted folder.
How to Zip File PowerShell on Windows 10/11
Of course, you can use the PowerShell compress archive command to create a zipped file/folder on Windows 10/11. Here we summarize some common examples of the PowerShell zip files.
# Example 1. PowerShell Zip Files to a New Zipped Archive
Compress-Archive -LiteralPath D:PowerShell -DestinationPath C:Archives -Force

# Example 2. PowerShell Zip Files That Include the Root Directory
If you want to zip a directory and create an archive folder that contains the root directory and all its files/subdirectories, you can use the Path parameter instead.
Compress-Archive -Path D:PowerShell -DestinationPath C:ArchivesOffice2016.zip
# Example 3. PowerShell Zip Files That Exclude the Root Directory
Compress-Archive -Path D:PowerShell* -DestinationPath C:ArchivesOffice2016.zip
# Example 4. PowerShell Zip Only the Files in the Root Directory
Compress-Archive -Path D:PowerShell*.* -DestinationPath C:ArchivesOffice2016.zip
# Example 5. Add Files to a Created Archive Folder
If you want to add some files to the created archive folder, you can use the -Update parameter. For that:
Compress-Archive -Path D:PowerShell* -Update -DestinationPath C:Archives
Further reading: How to do if you delete a zipped file accidentally? Don’t worry. MiniTool Partition Wizard can help you restore the deleted file on various storage devices quickly. In addition, it can help you to back up partitions, rebuild MBR, migrate OS, convert NTFS to FAT without data loss, etc.
I’m trying to zip using PowerShell but it’s not excluding the files listed in the excluded_paths. I want to especially exclude all __pycache__ folder occurrences that can be nested in different levels of my python code base. Currently everything gets zipped including the files and folders that are supposed to be excluded.
$excluded_paths = @( "${parent_folder}/.vscode/*", "${parent_folder}/.git/*", "${parent_folder}/temp/*", "${parent_folder}/resources/documentation/*", "${parent_folder}/**/useful/*", "${parent_folder}/scripts/*", "${parent_folder}/*.template.*", "${parent_folder}/*TODO.*", "${parent_folder}/$(Get-Item -Path $MyInvocation.MyCommand.Path).Name"
)
$exclude_pycache = Get-ChildItem -Path $parent_folder -Recurse -Directory -Filter "__pycache__" | ForEach-Object { $_.FullName }
$excluded_paths += $exclude_pycache
Pause
$winrar_path = "C:\Program Files\WinRAR\WinRAR.exe"
#$exclusion_switches = $excluded_paths | ForEach-Object { "-x'$_'" }
#& $winrar_path a -r -ep1 $exclusion_switches "$output_zip" "${parent_folder}\*"
$exclusion_string = $excluded_paths -join ' '
& $winrar_path a -r -ep1 "-x$exclusion_string" "$output_zip" "${parent_folder}\*"I tried this:
$exclusion_switches = $excluded_paths | ForEach-Object { "-x'$_'" }
& $winrar_path a -r -ep1 $exclusion_switches "$output_zip" "${parent_folder}\*"and tried this one:
$exclusion_string = $excluded_paths -join ' '
& $winrar_path a -r -ep1 "-x$exclusion_string" "$output_zip" "${parent_folder}\*"Even a simple example:
& $winrar_path a -r -x__pycache__ "$output_zip" "${parent_folder}\*"does not work. It zips but it does not exclude all the __pycache__ folders.
Ok I tried
& $winrar_path a -r "-x*__pycache__*" "$output_zip" "${parent_folder}\*"and that worked but the WinRAR documentation did not mention to quote it.
What am I doing wrong?
c:\this\path\some_data.zip -DestinationPath c:\this\pathThis particular example unzips the file named some_data.zip from a specific folder into a folder with the path c:\this\path.
The original zip file is kept in the original folder.

We can see that the directory contains a single zip file named all_data.zip.
Suppose that we would like to unzip this zip file to the same folder that it currently exists in.

We can see that the zip file has been unzipped to the folder that it currently existed in.
We see that the zip file contained three individual files:
Also notice that the original zip file all_data.zip still exists in the folder.
Note: You can find the complete documentation for the Expand-Archive method here.
PowerShell: How to Rename File Extension of Multiple Files
PowerShell: How to List Files in Directory by Date
PowerShell: How to Replace Every Occurrence of String in File
Add-Type -assembly "system.io.compression.filesystem"
$compressionLevel = [System.IO.Compression.CompressionLevel]::Fastest
$zip = [System.IO.Compression.ZipFile]::Open(c:\temp\archive.zip, 'update')
$files = (Get-ChildItem c:\folder | where-object {$_.Attributes -ne 'Directory'}
$files| ForEach-Object {[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $_.FullName,($_.Name),$compressionLevel)}
$zip.Dispose()It works fine, but it’s listing all the files in PowerShell execution screen, which I do not like.
So I am getting this for every file being added to archive:
Archive : System.IO.Compression.ZipArchive
CompressedLength :
FullName : file1.txt
LastWriteTime : 11/16/2022 11:55:43 AM -05:00
Length :
Name : file1.txtIs there a way to avoid printing all that info during execution?
searched everywhere, but couldn’t find a solution
Here is how I am using try/catch, when compressing the files
Add-Type -assembly "system.io.compression.filesystem"
$compressionLevel = [System.IO.Compression.CompressionLevel]::Fastest
$zip = [System.IO.Compression.ZipFile]::Open(c:\temp\archive.zip, 'update')
$files = (Get-ChildItem c:\folder | where-object {$_.Attributes -ne 'Directory'}
ForEach ($file in $files){
try {
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $_.FullName,($_.Name),$compressionLevel)
} catch { # cannot access file
Write-Warning "Could not archive $($_.FullName). `n $($_.Exception.Message)"
$Error_Message = $_.Exception.Message
write-warning $Error_Message
write-output "$(get-date) :ERROR Something went Wrong. ErrorMessage : $Error_Message " | out-file err_log.txt -Append -Force;
}Finally{
$zip.Dispose()
}
}I am getting the error message, which is pointing to a file in $Error_Message.
$Error_Message:
Exception calling “CreateEntryFromFile” with “4” argument(s): “The process cannot access the file ‘c:\folder\process.log’ because it is being used by another process.”
Same with “compress-archive” method:
try {
Compress-Archive -Path (Get-ChildItem c:\folder | where-object {$_.Attributes -ne 'Directory'}).FullName -DestinationPath 'c:\temp\archive.zip'
} catch { # cannot access file
Write-Warning "Could not archive $($_.FullName). `n $($_.Exception.Message)"
$Error_Message = $_.Exception.Message
write-warning $Error_Message
write-output "$(get-date) :ERROR Something went Wrong. ErrorMessage : $Error_Message " | out-file err_log.txt -Append -Force;
} finally{
}I do see the error on the screen – “The process cannot access the file ‘c:\folder\process.log’ because it is being used by another process.” , but $Error_Message is below:
$Error_Message:
Exception calling “.ctor” with “1” argument(s): “Stream was not readable.”
Not sure how to make compress-archive method to work and grab proper error message.



