Table of Contents
The problem
Currently I am stumbling upon a directory in Windows (11) filled with zip files that I wish to unpack. I want to unzip multiple files to their own separate directory, automatically. Although having some knowledge of computers, I have actually no idea if Windows can do this for me out of the box. When selecting multiple files and opening the context menu with a right click, the “Extract all” functionality will unpack only one archive to a newly created directory.
Pondering
I am not sure if I am missing some functionality somewhere in the operating system, but I have set my mind to do it my own way now. I have 7zip installed and I am curious if it has some command line API with which I can automate this process of unpacking the way I want it. I wouldn’t even know what Windows is using behind the scenes to unpack my files nor do I feel I want to do the effort to find out.
After opening a PowerShell terminal window, I try to access 7zip with it. At this point, I am again clueless of the name of the executable that I need in order to run commands on it. So, I am browsing to the installation folder and notice that 7zip has a 7z.exe file, but I also notice a 7zFM.exe and a 7zG.exe. After a quick online search, I found out that I need the 7z.exe file to run commands on and altogether I learned that it is possible to do so, because I wasn’t quite sure. At this point it seems like a good idea to make 7zip accessible via the Windows PATH, so that I don’t need to full location of the program to access it.
Adding 7z.exe to the PATH
After right clicking the Windows start screen logo and clicking on System, there is an Advanced system settings button in the Related links section under Device specifications. A System properties window opens and to add 7zip to the PATH, we open the Environment Variables.
Under System variables is the variable called: Path, which we can Edit. Clicking on the Edit button (after having selected the Path variable) opens up a new window where a “New” button gives the possibility to add a path to the list of paths. I added C:\Program Files\7-Zip to the end of the list.
Using 7zip with the terminal
The 7z command is not yet accessible in the PowerShell terminal, but after closing the terminal and reopening it, it will be. After typing 7z now, it will respond with options on how to use it.
The solution
We now have all the information we need to make 7zip automatically extract all (zip) archives with a little help of PowerShell:
Get-ChildItem -Filter "*.zip" | % { 7z x -o"$($_.BaseName)" $_.FullName }
PowerShellRemember that the script needs to be run from the same directory as the archives you want to extract.
Breakdown
If you want a bit of a breakdown of what is happening:
The Get-ChildItem is a Powershell command that iterates over all the items in the current directory of the terminal, only selecting zip files because of the -Filter “*.zip” parameter. A list of items is then transferred with the pipeline operator: “|” (the vertical bar) to the next command. The “%” sign is a shortcut of:
“ForEach-Object“. For each item (or object) in the list, 7zip will be invoked, where the x parameter expects files with full paths and the -o switch creates a directory with the BaseName of the item, omitting both the path and extension. The “$($_.BaseName)” syntax might seems a little weird, but the $( ) (subexpression syntax) is necessary to evaluate the $_.BaseName variable into a string which can be interpreted by 7zip. Otherwise, 7zip would read “$_.BaseName” literally. The $_.FullName is the full path of the archive we want to extract. Notice that $_ is used to refer to current item in the ForEach-Object loop.
So this concludes how you can unzip multiple files to their own separate directory.