Solved

Unzip GZ files using Powershell

Posted on 2012-12-31
9
2,329 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 69

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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 69

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 69

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 69

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
A procedure for exporting installed hotfix details of remote computers using powershell
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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