Thursday 15 January 2009

PowerShell script to unzip many files

Yeah, I know, I keep saying I’ll post more and all I do is post less.  Well the back half of 2008 kind of got derailed by unavoidable ‘life’ stuff and to be quite honest I haven’t yet decided if blogging will be one of my priorities in 2009.  Rest assured that if it is, I’ll be doing it properly and a redesign will be part of that commitment.  For now though, I’m back to using this as a holding space.  If I don’t want to lose something, and it might come in handy for someone else, it’ll end up in the blog.  Otherwise... well, we’ll see.

So here it is.  Say you have a folder full of zip files at j:\stuff.  Running this script (with j:\stuff as the current location) will unzip the contents of those zip files to j:\stuff.

$shell=new-object -com shell.application

$CurrentLocation=get-location

$CurrentPath=$CurrentLocation.path

$Location=$shell.namespace($CurrentPath)

$ZipFiles = get-childitem *.zip

$ZipFiles.count | out-default

foreach ($ZipFile in $ZipFiles)

{

$ZipFile.fullname | out-default

$ZipFolder = $shell.namespace($ZipFile.fullname)

$Location.Copyhere($ZipFolder.items())

}

If you also want to delete the source zip files, you can add remove-item $ZipFile after the line beginning $Location.Copyhere.  The reason I didn’t include that in the sample above is I strongly advise you (see my usual disclaimer) to run the script and make sure you’re satisfied the zip files unzipped properly before you go letting it delete anything!

Enjoy.

Creative Commons License This work by TechieBird is licensed under a Creative Commons Attribution-No Derivative Works 2.0 UK: England & Wales License.