Skip to content

Instantly share code, notes, and snippets.

@lowleveldesign
Last active August 2, 2024 08:45
Show Gist options
  • Save lowleveldesign/9d89466ce08c951bade0f6e30b0bf4cc to your computer and use it in GitHub Desktop.
Save lowleveldesign/9d89466ce08c951bade0f6e30b0bf4cc to your computer and use it in GitHub Desktop.
Download a file and unzip it in some folder if it's an archive.
param (
[Parameter(Mandatory = $true)][string]$Uri,
[Parameter(Mandatory = $true)][string]$TargetFolderPath
)
$ErrorActionPreference = "Stop"
$FileName = Split-Path -Path $Uri -Leaf
$DownloadFilePath = Join-Path $env:TEMP $FileName
Write-Host "Downloading file from $Uri to $DownloadFilePath"
Invoke-WebRequest -UseBasicParsing -Uri $Uri -OutFile $DownloadFilePath
if ($FileName.EndsWith(".zip")) {
Expand-Archive -Force -LiteralPath $DownloadFilePath -DestinationPath "$TargetFolderPath"
Remove-Item -LiteralPath $DownloadFilePath
} else {
Move-Item -Force -LiteralPath $DownloadFilePath -Destination $TargetFolderPath
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment