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.
February 12th, 2013 9:23pm
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())
}
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.
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
February 13th, 2013 1:19am
Sometimes it's the simplest things ;-)
February 13th, 2013 1:25am
Yep - thanks for your help!
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.
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
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