Powershell script to extract Cab file contents and move files to new locations

user3745479 picture user3745479 · Jun 16, 2014 · Viewed 9.3k times · Source

I've been given 14K CAB files each containing 200 files which need to be unzipped into their original locations.

Unfortunately it's not as easy as all of them being extracted to the same location :-(

I've decided to use PowerShell and have generated a list of individual file locations for each file using SQL and can extract the CABs, unfortunately they all extract to the current location.

I am trying to move them to their respective locations, but am struggling.

Here's the code, I've got so far

$shell_app=new-object -com shell.application
$CABfilename= Import-CSV "CABFileList.csv" -Header CABfilename | Foreach-object {

$zip_file = $shell_app.namespace((Get-Location).Path + "\$CABfilename")
$destination = $shell_app.namespace((Get-Location).Path)
$destination.Copyhere($zip_file.items())

$dvs = Import-csv "CABFileList.csv" -Header Path, DVSFilename | 

Foreach-object{
    Move-item $_.DVSFilename* $_.Path
}

Answer

Simon C picture Simon C · Nov 25, 2016

This is an old question, but someone might find the answer useful anyway. I have adapted one I made today to download all WSPs from a farm and extract their contents.

$CABfilename = Import-CSV "CABFileList.csv" -Header CABfilename | Foreach-object {
 # Grab the Solution
$Path = $SaveLocation + $CABfilename
# Check the path is ok
$Path
# Make a copy with extension '.cab' for extraction
$DotCab = $CABfilename + ".cab"
$SolutionDir = $Dotcab -replace '.wsp.cab'
mkdir $SolutionDir
copy-item $CABfilename $DotCab
# Now extract it, assuming you have expand.exe in the filsystem (should be in everything post Server 2008 / Vista)
if(C:\Windows\System32\expand.exe)     {
    try { cmd.exe /c "C:\Windows\System32\expand.exe -F:* $Dotcab $SolutionDir"}
   catch { Write-host "Nope, don't have that, soz."}
}}