Skip to content

Instantly share code, notes, and snippets.

@CodeByAidan
Last active February 23, 2025 05:36
Show Gist options
  • Save CodeByAidan/fb2707306355f6fa4eca41388723b5f7 to your computer and use it in GitHub Desktop.
Save CodeByAidan/fb2707306355f6fa4eca41388723b5f7 to your computer and use it in GitHub Desktop.
simple unzip command for Windows Powershell. Add it to your $PROFILE via `Set-Alias -Name unzip -Value Expand-ZipArchive -Scope Global -Description "Archive extraction shortcut"`
function Expand-ZipArchive {
<#
.SYNOPSIS
Extracts the contents of a ZIP archive to a specified directory.
.DESCRIPTION
The Expand-ZipArchive function expands a ZIP archive to a target directory,
with comprehensive error handling and automatic directory creation.
By default, extracts to a subdirectory matching the archive name in the same location.
.PARAMETER zipFile
Path to the ZIP archive file. Must exist and have a .zip extension.
.PARAMETER destination
Target directory for extraction. Created if it doesn't exist.
Defaults to a subdirectory named after the archive in the same location.
.EXAMPLE
PS> Expand-ZipArchive "C:\archive.zip"
Extracts to C:\archive\ directory.
.EXAMPLE
PS> Expand-ZipArchive "backup.zip" -destination "D:\restored"
Extracts to D:\restored directory.
.EXAMPLE
PS> Get-ChildItem *.zip | ForEach-Object { Expand-ZipArchive $_ }
Batch extract multiple archives.
#>
[CmdletBinding()]
param (
[Parameter(
Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = "Path to ZIP archive file"
)]
[ValidateScript({
# Validate file existence
if (-not (Test-Path -Path $_ -PathType Leaf)) {
throw "ZIP file not found: '$_'"
}
# Validate ZIP extension
$extension = [System.IO.Path]::GetExtension($_)
if ($extension -ne ".zip") {
throw "Invalid file extension '$extension' - must be .zip"
}
$true
})]
[Alias("FilePath")]
[string] $zipFile,
[Parameter(
Position = 1,
HelpMessage = "Target extraction directory"
)]
[ValidateScript({
# Prevent using existing files as destination
if (Test-Path -Path $_ -PathType Leaf) {
throw "Destination path '$_' is an existing file"
}
$true
})]
[string] $destination
)
begin {
# Configure strict error handling
$ErrorActionPreference = 'Stop'
}
process {
try {
# Resolve absolute ZIP file path
$zipItem = Get-Item -Path $zipFile
$zipFullPath = $zipItem.FullName
# Set default destination if not specified
if (-not $PSBoundParameters.ContainsKey('destination')) {
$destination = Join-Path -Path $zipItem.DirectoryName `
-ChildPath $zipItem.BaseName
}
Write-Verbose "Preparing to extract '$zipFullPath' to '$destination'"
# Ensure destination directory exists
if (-not (Test-Path -Path $destination -PathType Container)) {
Write-Verbose "Creating destination directory: $destination"
$null = New-Item -Path $destination `
-ItemType Directory `
-Force `
-ErrorAction Stop
}
# Perform archive extraction
Expand-Archive -Path $zipFullPath -DestinationPath $destination -Force
Write-Host "Extraction completed: '$destination'" -ForegroundColor Green
}
catch {
Write-Host "Error extracting archive: $_" -ForegroundColor Red
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment