Unzipping files in powershell

I have files with varying names that periodically land in C:\tmp. I wont know the name of the files before they land, so I want to unzip any file with a .zip extension.

I would like to know how to unzip these files and move to C:\tmp\unzipped

Thanks in advance for the help.

February 12th, 2013 9:13pm

Get-ChildItem C:\tmp\*.zip | % {<insert your favorite zip utility here>; Move-Item $_ C:\tmp\unzipped}
PowerShell Community Extensions has cmdlets for working with ZIP files.  Alternatively 7zip is a good command line utility.
Free Windows Admin Tool Kit Click here and download it now
February 12th, 2013 9:23pm

Here's an example using Windows native zip functionality...

http://social.technet.microsoft.com/Forums/en/winserverpowershell/thread/c471780f-6c08-4c0b-9478-e51d196b6a78

February 12th, 2013 9:25pm

Here is how I solved it however I have to hit the enter key to get the unzip to happen, Is there a way to force it?

$filelocation = dir C:\tmp\*.zip
foreach ($file in $filelocation){
$filename = $file.name.ToString()
$shell_app=new-object -com shell.application
$zip_file = $shell_app.namespace("C:\tmp\$filename")
$destination = $shell_app.namespace("C:\tmp\unzipped test\")
#unzip the files
$destination.Copyhere($zip_file.items())
}

Free Windows Admin Tool Kit Click here and download it now
February 13th, 2013 12:44am

Again, I appreciate the help.

February 13th, 2013 12:44am

At what point do you have to hit the enter key?

Do you get a prompt on your screen?

Does the folder 'c:\tmp\unzipped test\' exist already?

That code runs with no interruption for me - but I did create the destination folder first.

Free Windows Admin Tool Kit Click here and download it now
February 13th, 2013 12:54am

I have to hit enter after I copy the script to PowerShell. The script does not appear to run with a .cmd file calling the .ps1 file.

The c:\tmp\unzipped test folder does already exist.

Here is the cmd file syntax

@echo 0ff

start /min %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -command "& 'C:\scripts\NJ unbundle.ps1' "

pause
Exit

  • Edited by goosebags Tuesday, February 12, 2013 10:09 PM
February 13th, 2013 1:03am

I was able to get it to run. I am local on my machine and forgot to set the execution policy to unrestricted.

Set-ExecutionPolicy Unrestricted
Free Windows Admin Tool Kit Click here and download it now
February 13th, 2013 1:19am

Sometimes it's the simplest things ;-)
February 13th, 2013 1:25am

Yep - thanks for your help!
Free Windows Admin Tool Kit Click here and download it now
February 13th, 2013 1:38am

check out the new 

PowerShell 5.0 cmdlets:

Manage .ZIP files with new cmdlets

Two new cmdlets, Compress-Archive and Expand-Archive, let you compress and expand ZIP files.

Compress-Archive

The Compress-Archive cmdlet creates a new archive file from specified files. An archive file allows multiple files to be packaged and optionally compressed into a single file for easier handling and storage. An archive file can be compressed by using a compression algorithm specified in the -CompressionLevel parameter.

Compress-Archive -LiteralPath <String[]> [-DestinationPath] <String> [-Update] [-CompressionLevel <Microsoft.PowerShell.Commands.CompressionLevel>]

Compress-Archive [-Path] <String[]> [-DestinationPath] <String> [-Update] [-CompressionLevel <Microsoft.PowerShell.Commands.CompressionLevel>]

Expand-Archive

The Expand-Archive cmdlet extracts files from a specified archive file. An archive file allows multiple files to be packaged and optionally compressed into a single file for easier handling and storage.

Expand-Archive -LiteralPath <String> [-DestinationPath] <String>

Expand-Archive [-Path] <String> [-DestinationPath] <String>


September 17th, 2014 10:54pm

Be warned, as of right now WMF 5.0 is still in preview only.
Free Windows Admin Tool Kit Click here and download it now
September 17th, 2014 10:58pm

What if I want to extract only a particular file out of the given zip file if I already know the name of that file?
February 12th, 2015 2:26am

There's a script in the repository which does that and more...

https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Get-Specific-9b35352f

Free Windows Admin Tool Kit Click here and download it now
February 12th, 2015 2:56am


[System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')
[System.IO.Compression.ZipFile]::ExtractToDirectory($sourceFile, $targetFolder)


February 12th, 2015 4:39am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics