Script - Unzip cab and zip files

The Dell driver packs located on Dells community site have recently had problems being decompressed I would get errors that would stop the decompression process. I have found that PowerShell can decompress the files correctly. So I created a script to decompress cab and zip files that works very well when you are dealing with large files. Like most of my scripts you can run it right from a PowerShell window and use the File and DecompressTo parameters or you can run the script with out the parameters to display a GUI.

This script is used to extract a cab or zip file 

The Script will launch a GUI that will allow you to choose browse for a zip or cab file. Then select a folder to extract to.
You can also lauch the script with out a GUI 

Specifyies the zip or cab file

.PARAMETER DecompressTo
Specifyies the location where the zip or cab should be decompressed to, if you do not specify this paramiter C:\Extracted_Files 
will be used by default

Extract.ps1 -File C:\
This will extract the cab file to C:\Exreacted_files and will not show the GUI

Extract.ps1 -File C:\ -DecompressTo C:\Example
Extracts the zip file to C:\Example and will not show the GUI

Created By: Kris Gross
Twitter: @kmgamd


    $DecompressTo = "C:\Extracted_Files"

#Generates the form
Function Generate-Form {

#Needed for the form to show
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName
#Set the standrads of the form window
$form = New-Object System.Windows.Forms.Form 
$form.Text = "Decompresstion Tool - V"
$form.Size = New-Object System.Drawing.Size(460,280) 
$form.StartPosition = "CenterScreen"

#sets the ok button
$ExtractButton = New-Object System.Windows.Forms.Button
$ExtractButton.Location = New-Object System.Drawing.Point(50,180)
$ExtractButton.Size = New-Object System.Drawing.Size(300,23)
$ExtractButton.Text = "Decompress"

#sets the Browse Button for the File
$FindFileButton = New-Object System.Windows.Forms.Button
$FindFileButton.Location = New-Object System.Drawing.Point(320,70)
$FindFileButton.Size = New-Object System.Drawing.Size(75,23)
$FindFileButton.Text = "Browse"

#sets the Browse Button for the Folder
$FindFolderButton = New-Object System.Windows.Forms.Button
$FindFolderButton.Location = New-Object System.Drawing.Point(320,140)
$FindFolderButton.Size = New-Object System.Drawing.Size(75,23)
$FindFolderButton.Text = "Browse"

#Sets the box showing the File selected
$FileBox = New-Object System.Windows.Forms.TextBox 
$FileBox.Location = New-Object System.Drawing.Point(24,80)
$FileBox.Size = New-Object System.Drawing.Size(260,22)
$FileBox.Font = 'Lucida Console' 
$FileBox.Text = $FileText

#Sets the box showing the selected folder
$FolderBox = New-Object System.Windows.Forms.TextBox
$FolderBox.Location = New-Object System.Drawing.Point(24,140)
$FolderBox.Size = New-Object System.Drawing.Size(260,22)
$FolderBox.Font = 'Lucida Console'
$FolderBox.Text = $FolderText

#label for file box
$FileBoxLabel = New-Object System.Windows.Forms.Label
$FileBoxLabel.Location = New-Object System.Drawing.Point(24,60)
$FileBoxLabel.Size = New-Object System.Drawing.Size(280,13) 
$FileBoxLabel.Text = "1. File:"

#Label for folder box
$ExtractLabel = New-Object System.Windows.Forms.Label
$ExtractLabel.Location = New-Object System.Drawing.Point(24,120)
$ExtractLabel.Size = New-Object System.Drawing.Size(280,13) 
$ExtractLabel.Text = "2. Decompress To:"

#Label for the form
$FormLabel = New-Object System.Windows.Forms.Label
$FormLabel.Location = New-Object System.Drawing.Point(24,20)
$FormLabel.Size = New-Object System.Drawing.Size(280,13)
$FormLabel.Text = "Use this tool to decompress a zip or cab file"

#tells the progress of the extraction process
$RunningLabel = New-Object System.Windows.Forms.Label
$RunningLabel.Location = New-Object System.Drawing.Point(24,215)
$RunningLabel.Size = New-Object System.Drawing.Size(280,13)
$RunningLabel.ForeColor = "Red"
$RunningLabel.Text = "decompressing files"
$RunningLabel.Visible = $false

#add all resorces of the form 

#Show the Form 

#action taken then extract is pressed
function Click_Extract
    $File = $FileBox.Text
    $DecompressTo = $FolderBox.Text
    #Gets the file extention for the selected file
    $Extention = [System.IO.Path]::GetExtension("$File")
    #performs action based on file extention
    if ($Extention -eq ".cab") 
        $RunningLabel.Visible = $true
        expand $File -f:* $DecompressTo
    if ($Extention -eq ".zip")
        $RunningLabel.Visible = $true
        [io.compression.zipfile]::ExtractToDirectory($file, $DecompressTo)
    $RunningLabel.Text ="Completed"
    $RunningLabel.ForeColor = "Green"
    $FolderBox.Text = ""
    $FileBox.Text = ""

#form used to select the file
Function Get-FolderPath
    param([string]$Description="Select Folder",[string]$RootFolder="Desktop")
    [System.Reflection.Assembly]::LoadWithPartialName("") | Out-Null     
    $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
    $objForm.Rootfolder = $RootFolder
    $objForm.Description = $Description
    $FolderText = $objForm.SelectedPath

#form used to select the folder
Function Get-FilePath 
    [String]$Filter = "|*.*",
    [String]$InitialDirectory = "C:\")
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $InitialDirectory
    $OpenFileDialog.filter = $Filter
    #Runs the form function over so the results are added to the text box
    $FileText = $OpenFileDialog.filename

#action taken when the GUI is not used
function extract
    #Crates the Extract to folder if it dose not exist
    if (($DecompressTo)){mkdir $DecompressTo}
    #gets the slected file extention
    $Extention = [System.IO.Path]::GetExtension("$File")
    #preforms a action based on selected file extention
    if ($Extention -eq ".cab") 
        expand $File -f:* $DecompressTo
    if ($Extention -eq ".zip")
        [io.compression.zipfile]::ExtractToDirectory($file, $DecompressTo)

#Call the Function based on paramiters
if (($Cab)){Extract} else {Generate-Form}

Popular posts from this blog

SCCM Task Sequence GUI - How to set up the TS to work with a GUI

SCCM Applications vs. SCCM Packages: Understanding the Key Differences

How to Deploy a Windows 10 Servicing update as a Application