Solved

Unzip GZ files using Powershell

Posted on 2012-12-31
9
2,121 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
  • 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 68

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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 68

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 68

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 68

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
Set OWA language and time zone in Exchange for individuals, all users or per database.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now