Author Posts

September 11, 2018 at 5:42 pm

Hi everyone, new to PowerShell and trying to accomplish something I know if possible but can't quite get it. I need to create a script that can be run as a Scheduled Task that will look for a ZIP file in a specific folder and unzip it to a folder named exactly what the zip file is named. Using Expand-Archive, I'm able to get a zip file with a KNOWN filename unzipped and even a directory created with the same name as the zip file.

What I can't seem to figure out is, how can I have my script find any zip file, then unzip it to a folder that it creates with the same name as the original zip? The source directory will always be the same.

September 11, 2018 at 5:47 pm

You might show the code you already have. It's easier to improve or extend a script than creating something completeley new. 😉

September 11, 2018 at 5:50 pm

It would be great if you try it yourself first. We are here to help you on any issues you face with the script you write. If you have already started, please share a sample here, we will help you in figuring out the problem.

September 11, 2018 at 7:18 pm

Use something like Get-ChildItem to get a list of zip files and iterate over it to figure out the file names. The objects returned by that command have several properties you can access, which you can view in a list by piping to Get-Member and examining the output.

Using those you can determine the necessary folder names. 🙂

September 11, 2018 at 7:26 pm

ls *.zip | foreach { expand-archive $_ }

September 11, 2018 at 9:08 pm

As for this...

ls *.zip | foreach { expand-archive $_ }

This will simply expand things where they are.

Take what you are doing to create the folder on the fly, using the zipname returned and then expand in that new folder. This is actually shown in the built-in help files, for you to use directly or tweak as needed.

 

Get-ChildItem -Path D:\ZIPFiles | Format-Table -AutoSize

    Directory: D:\ZIPFiles


Mode          LastWriteTime  Length Name    
----          -------------  ------ ----    
-a----   9/4/2018   5:36 PM 2807568 test.zip 


Get-ChildItem -Path D:\ZIPFiles -Filter '*.zip' | 
ForEach{ 
            Expand-Archive -Path D:\ZIPFiles\$($_.Name) -DestinationPath D:\ZIPFiles\$($_.Basename) -Force 
            Get-ChildItem -Path D:\ZIPFiles\$($_.Basename) -Recurse
       }

September 12, 2018 at 2:27 am

Actually, my example makes a subfolder using the basename of the zip file.