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.




<#
.SYNOPSIS
This script is used to extract a cab or zip file 

.DESCRIPTION
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 

.PARAMETER File
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

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

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

.NOTES
Created By: Kris Gross
Contact: Krisgross@sccmtst.com
Twitter: @kmgamd


.LINK
http://www.sccmtst.com/
#>


Param(
    $File,
    $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 system.io.compression.filesystem
#Set the standrads of the form window
$form = New-Object System.Windows.Forms.Form 
$form.Text = "Decompresstion Tool - V 1.0.0.2"
$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"
$ExtractButton.Add_Click({Click_Extract})

#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"
$FindFileButton.Add_Click({Get-FilePath})

#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"
$FindFolderButton.Add_Click({Get-FolderPath})

#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 
$form.Controls.Add($ExtractButton)
$form.Controls.Add($FindFileButton)
$form.Controls.Add($FindFolderButton)
$form.Controls.Add($FileBox)
$form.Controls.Add($ExtractLabel) 
$form.Controls.Add($FileBoxLabel) 
$form.Controls.Add($FolderBox)
$form.Controls.Add($FormLabel)
$form.Controls.Add($RunningLabel)

#Show the Form 
[void]$form.ShowDialog() 
}

#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
{
    [CmdletBinding()]
    param([string]$Description="Select Folder",[string]$RootFolder="Desktop")
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null     
    $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
    $objForm.Rootfolder = $RootFolder
    $objForm.Description = $Description
    [void]$objForm.ShowDialog()
    $objForm.SelectedPath
    $FolderText = $objForm.SelectedPath
    $Form.Close()
 $Form.Dispose()
    Generate-Form
}

#form used to select the folder
Function Get-FilePath 
{
    [CmdletBinding()]
    Param(
    [String]$Filter = "|*.*",
    [String]$InitialDirectory = "C:\")
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $InitialDirectory
    $OpenFileDialog.filter = $Filter
    [void]$OpenFileDialog.ShowDialog()
    $OpenFileDialog.filename
    #Runs the form function over so the results are added to the text box
    $FileText = $OpenFileDialog.filename
    $Form.Close()
 $Form.Dispose()
    Generate-Form
}

#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

Faster PXE boot times in SCCM 1606 and later