?
Solved

Unzip GZ files using Powershell

Posted on 2012-12-31
9
Medium Priority
?
3,054 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 400 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 1600 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

WatchGuard's M Series Appliances - Miecom Approved

WatchGuard's newest M series appliances were put to the test by Miercom.  We had great results and outperformed all of our competitors in both stateless and stateful traffic throghput scenarios! Ready to see how your UTM appliance stacked up? Download the Miercom Report!

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

777 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