Skip to content

Instantly share code, notes, and snippets.

@mkropat mkropat/Get-WebFile.ps1

Last active Feb 22, 2020
What would you like to do?
function Get-WebFile {
[string] $OutFile,
[string] $Hash,
[string] $HashAlgorithm = 'SHA256'
if (-not $OutFile) {
$OutFile = Split-Path -Leaf $Uri
$meta = Get-Item $OutFile -ErrorAction SilentlyContinue
if ($meta -and $meta.PSIsContainer) {
throw '$OutFile may not be a directory'
$container = Split-Path $OutFile
if (-not $container) {
$container = '.'
$tempFile = Join-Path $container "$(New-Guid).part"
Invoke-WebRequest $Uri -OutFile $tempFile -ErrorAction Stop
if ($Hash) {
$actual = Get-FileHash $tempFile -Algorithm $HashAlgorithm
if ($Hash -ne $actual.Hash) {
throw "Downloaded file does not have the expected $HashAlgorithm hash: $Hash"
Move-Item $tempFile $OutFile -Force
Get-Item $OutFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.