If you are in a hurry, just copy paste one of the below scripts in PowerShell screen and hit ENTER!
ls *.zip | ForEach-Object -Process {tar -xf $_.Name; Write-Host $_.Name, “ is extracted”;}
Or
ls *.zip | ForEach-Object -Process {Expand-Archive -Path $_.Name -DestinationPath ../ ; Write-Host $_.Name, “ is extracted”;}
Okay! Now, you have sometime to read the background and the details of the script above.
Recently, I encountered a situation on a Windows machine where I had to extract 20+ .zip files. I tried the default way of “Extract All..” context menu but, if you select more than one zip file and “Extract All…”, it is possible to extract only one file - the one which is “selected” within the selection.
There doesn't seem to be a straight-forward way to extract multiple files at one go. I think, this should be possible with software programs for compression such as WinZip. However, if the working environment is a rather restricted one, where all such external software programs are not available, PowerShell could be used for multiple files extraction.
Note: In many restricted environments, PowerShell is also limited but the cmdlets used in the above scripts should be available, by default. If not, I am really sorry for you 🤭!
Now, let's take a closer look at both the scripts.
1
ls *.zip | ForEach-Object -Process {tar -xf $_.Name; Write-Host $_.Name, “ is extracted”;}
In this approach,
ls *.zip lists only the files with extension zip. If other types of compressed files to be processed such as tar, the extension parameter *.zip must be adjusted according to the requirement.
Explore the other options that the ls alias provides, such as recursive search for the compressed files. The ls is indeed an alias for the cmdlet Get-ChildItem. Try Get-Help ls or Get-Help Get-ChildItem for more information about this cmdlet.
The pipe symbol '|' helps us redirecting the output of the ls *.zip command to the next cmdlet ForEach-Object. It is nothing new for the Unix users (also the rare species of MS-DOS world - like me🤓). For others, with this piping allows us to access the output of the first program, here the ls cmdlet which outputs the filenames with their full path information.
The ForEach-Object cmdlet cycles through all the files that are provided by ls cmdlet.
-
The -Process parameter invokes the process mentioned next. In this case the tar utility with -xf parameters.
- x refers eXtract
- f refers the file name that is mentioned next
The next parameter $_ refers the current file object obtained from the ForEach-Object's iterations.
The .Name is the property whose value contains the file name of that iteration. This will be used by the process tar and with -xf mentioned, the utility will extract files that resides in the mentioned file name.
The ForEach-Object ensures to cycle through all the .zip files in the current directory once after another and executes the tar utility for each of them. The Write-Host cmdlet prints the name of the file just processed, with the string "is extracted". This is just to see the progress if there are many number of .zip files in the current folder.
Note: In case of any errors in extracting, this script will just proceed with the .zip file that is found subsequently.
2
This second approach is similar to the first one except that, instead of the tar utility, this uses the PowerShell cmdlet Expand-Archive with the self-explanatory parameters -Path and -DestinationPath.
This indeed let us being independent of the tar utility if it itself is not available in the working environment. However, this seems to be having its own limitations such as supporting only .zip files and a size limit of 2GB. I let you decide which approach is more suitable for you!
Top comments (0)