Batch-Unzipping Files
Thanks to https://serverfault.com/questions/18872/how-to-zip-unzip-files-in-powershell/201604#201604 for the core code.
Here's a function to unzip files, with rudimentary error handling, and ability to specify source and destination from the command line. It also brute-forces the "overwrite flag fails on pre-Win7" issue by allowing -force to unzip to a temp folder, then moves the contents of that folder forcibly over the destination, effectively overwriting the existing files if possible.
function Unzip-Item {
param (
[String[]]$path = $([String[]](Get-ChildItem "*.zip")),
$dest = '.',
[switch]$force
);
if ($force) {
$oldDest = $dest;
$dest = "$env:temp\unzip-item_{0}" -f (Get-Date -Format 'yyyyMMddhhmmss');
mkdir $dest | Out-Null;
}
$shApp = New-Object -ComObject Shell.Application;
$destObj = $shApp.NameSpace((Resolve-Path $dest).Path);
if (!$path) {
Write-Warning "-path does not contain any zip files. Returning";
return;
}
foreach ($_ in $path) {
$zipPath = (Resolve-Path $_).Path;
if (!($zipPath -match "\.zip$")) {
Write-Warning "-path $_ is not a .zip file. Skipping.";
continue;
}
$zipObj = $shApp.NameSpace($zipPath);
if ($zipObj) {
$destObj.CopyHere($zipObj.Items());
} else {
Write-Warning "Unable to find file $_. Skipping.";
}
}
if ($force) { Move-Item -Path "$dest\*" -Destination $oldDest -Force; }
}