Functions/Expand-7Zip.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<#
.SYNOPSIS
Extract contents of a compressed archive file
.DESCRIPTION
Use Expand-7Zip to extract the contents of an archive.
.EXAMPLE
Expand-7Zip archive.zip

Extract contents of archive.zip in the current working folder

.EXAMPLE
Expand-7Zip archive.zip -DestinationPath c:\archive

Extract contents of archive.zip in the c:\archive folder

.EXAMPLE
Expand-7Zip "c:\folder\files.gz"

Extract contents of c:\folder\files.gz into current working folder

.PARAMETER FullName
The full path of the compressed archive file.
.PARAMETER DestinationPath
The output directory.
.PARAMETER Remove
If $True this will remove the compressed version of the file only leaving the uncompressed contents.
.LINK
http://gavineke.com/PS7Zip/Expand-7Zip
#>

Function Expand-7Zip {
    [CmdletBinding(HelpUri='http://gavineke.com/PS7Zip/Expand-7Zip')]
    Param(
        [Parameter(Mandatory=$True,Position=0,ValueFromPipelineByPropertyName=$True)]
        [ValidateScript({$_ | Test-Path -PathType Leaf})]
        [System.IO.FileInfo]$FullName,

        [Parameter()]
        [Alias('Destination')]
        [ValidateNotNullOrEmpty()]
        [string]$DestinationPath,

        [Parameter()]
        [switch]$Remove
    )
    
    Begin {}
    
    Process {
        Write-Verbose -Message 'Extracting contents of compressed archive file'
        If ($DestinationPath) {
            & "$7zaBinary" x -o"$DestinationPath" "$FullName"
        } Else {
            & "$7zaBinary" x "$FullName"
        }

        If ($Remove) {
            Write-Verbose -Message 'Removing compressed archive file'
            Remove-Item -Path "$FullName" -Force
        }
    }
    
    End {}
    
}