Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Unzip GZ files using Powershell

Posted on 2012-12-31
9
Medium Priority
?
3,301 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 71

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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

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 71

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 71

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 71

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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

609 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