Solved

Unzip GZ files using Powershell

Posted on 2012-12-31
9
2,724 Views
Last Modified: 2013-02-05
Hello

I have  Windows 2008 Server with some GZ type files in a folder. I would like to script unzipping them using Powershell, can someone tell me if this is possible and how I would do it?

The folder is d:\data_files\ and I'd like to uncompress all the .gz files in there.
0
Comment
Question by:tonyrobbins1978
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 12

Accepted Solution

by:
Gary Dewrell earned 100 total points
ID: 38733004
You can use powershell to call the CLI of any decompressoin program. I use powershell to run 7zip all the time.
0
 

Author Comment

by:tonyrobbins1978
ID: 38733302
Thanks I hada look at 7zip

I see the command line stuff here: http://www.dotnetperls.com/7-zip-examples which talks about using the "e" command

What I'm not sure is how I'd call the CLI of 7zip in Powershell, get it to unzip those files, and then let the Powershell script do some other stuff with these unzipped files ?
0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 400 total points
ID: 38734573
It is as simple as this:
$folder = 'd:\data_files\'
$7za = 'C:\Program Files\7zip\7za.exe'

get-childitem $folder -filter *.gz | 
   foreach-object { & $7za e $_.FullName $folder }

Open in new window

You can include any other action you would like to perform in the foreach-object scriptblock, of course.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tonyrobbins1978
ID: 38734894
So this seems to work:

$folder = 'd:\data_files\'
$7za = 'C:\Program Files\7zip\7za.exe'

get-childitem $folder -filter *.gz |
   foreach-object { & $7za e  $folder }

But what happens is that it extracts the files to the folder I'm running the script from. I need the files to be extracted to $folder as well.

I tried

$folder = 'd:\data_files\'
$7za = 'C:\Program Files\7zip\7za.exe'

get-childitem $folder -filter *.gz |
   foreach-object { & $7za e  $folder -o$folder }

But what it does is create a folder named $folder to extract the files to! :-)

Any ideas guys?
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 38735150
-o$folder should work, but does not. Anyway, you need to provide a single archive with the syntax shown above, or a path including a file mask; that is why I used $_:
$folder = 'd:\data_files\'
$7za = 'C:\Program Files\7zip\7za.exe'

get-childitem $folder -filter *.gz | 
   foreach-object { & $7za e $_.FullName "-o$folder" }

Open in new window

or, much shorter
$folder = 'd:\data_files'
$7za = 'C:\Program Files\7zip\7za.exe'
& $7za e $folder\*.gz "-o$folder" 

Open in new window

which will process all *.gz files in one go, instead of looping thru them.
0
 

Author Comment

by:tonyrobbins1978
ID: 38735261
Perfect, I used the latter which works exactly as I wanted it! :-)

Could you explain what you changed? Also, what does the & character mean and do?
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 38735872
Because of the semantical parsing PowerShell applies, a dollar is sometimes not taken as the variable prefix, but instead literally. That is what happens if you just use   -o$folder. Putting a string into double quotes switches into a different parsing mode, which tries harder to find a variabe reference. Would the parameter allow for    -o $folder, which it does not, no issue had arised, as this is treated correctly by PS.

The ampersand operator is used for interpreting the next string as a command. Just using a variable reference as first token on a commandline does not work, PS is in "expression mode" when it sees that, and will treat the var content just as a string instead of a command. But it would not help if we used the path directly, it contains spaces, so would need some sort of escaping, which is usually done by ticks or quotes - and the same issue would arise, a string as first token will not be used as command. Or in short:
   & "C:\path to some exe\This is the.Exe"
runs the command,
   "C:\path to some exe\This is the.Exe"
just results in the string.

If you put the command into PATH, and the command does not contain special characters like spaces, you could of course just call it:
   7za e $folder\*.gz "-o$folder"
but not
   C:\Program Files\7-zip\7za.exe e $folder\*.gz "-o$folder"
0
 

Author Comment

by:tonyrobbins1978
ID: 38736106
Thanks Qlemo, my last question is the & sign short for something?
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 38736310
No, it is no "alias" for any command. But invoke-command is what comes most near.
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question