Functions/Expand-MyArchive.ps1
function Expand-MyArchive { [CmdletBinding()] param ( [Parameter(Mandatory = $true, Position = 0)] [string] $Path, [Parameter(Mandatory = $false, Position = 1)] [string] $DestinationPath ) #region Checks # Check if the path is a valid file and exists if (-not (Test-Path $Path)) { Write-Error "The specified path '$Path' does not exist." return } # Check if the path is a file if (-not (Test-Path $Path -PathType Leaf)) { Write-Error "The specified path '$Path' is not a file." return } # Check if the destination path is provided, if not, use the same directory as the archive with archive name if (-not $DestinationPath) { $archiveFile = Get-Item $Path $DestinationPath = $archiveFile.DirectoryName + "\" + $archiveFile.BaseName Write-Verbose $DestinationPath } # Check if the destination path exists, if not, create it if (-not (Test-Path $DestinationPath)) { New-Item -Path $DestinationPath -ItemType Directory -Force | Out-Null } #endregion Write-Verbose $Path Write-Verbose $DestinationPath if (Test-Path "C:\Program Files\7-Zip\7z.exe") { Write-Host "Using 7z.exe to extract the archive." -ForegroundColor Green Start-Process -FilePath "7z.exe" -ArgumentList "x", "-y", "-o`"$DestinationPath`"", "`"$Path`"" -NoNewWindow -Wait -ErrorAction Stop } else { Write-Host "7z.exe not found, using Expand-Archive." -ForegroundColor Yellow Expand-Archive -Path $Path -DestinationPath $DestinationPath -Force -ErrorAction Stop } } |