Private/Start-DownloadFile.ps1
function Start-DownloadFile { <# .SYNOPSIS Download a file from a given URL and save it in a specific location. .DESCRIPTION Download a file from a given URL and save it in a specific location. .NOTES Author: Nickolaj Andersen Contact: @NickolajA Created: 2020-01-04 Updated: 2020-01-04 Version history: 1.0.0 - (2020-01-04) Function created #> param( [parameter(Mandatory = $true, HelpMessage = "URL for the file to be downloaded.")] [ValidateNotNullOrEmpty()] [string]$URL, [parameter(Mandatory = $true, HelpMessage = "Folder where the file will be downloaded.")] [ValidateNotNullOrEmpty()] [string]$Path, [parameter(Mandatory = $true, HelpMessage = "Name of the file including file extension.")] [ValidateNotNullOrEmpty()] [string]$Name ) Begin { # Set global variable $ErrorActionPreference = "Stop" # Construct WebClient object $WebClient = New-Object -TypeName System.Net.WebClient } Process { # Create path if it doesn't exist if (-not(Test-Path -Path $Path)) { New-Item -Path $Path -ItemType Directory -Force | Out-Null } # Register events for tracking download progress $Global:DownloadComplete = $false $EventDataComplete = Register-ObjectEvent $WebClient DownloadFileCompleted -SourceIdentifier WebClient.DownloadFileComplete -Action {$Global:DownloadComplete = $true} $EventDataProgress = Register-ObjectEvent $WebClient DownloadProgressChanged -SourceIdentifier WebClient.DownloadProgressChanged -Action { $Global:DPCEventArgs = $EventArgs } # Start download of file $WebClient.DownloadFileAsync($URL, (Join-Path -Path $Path -ChildPath $Name)) # Track the download progress do { $PercentComplete = $Global:DPCEventArgs.ProgressPercentage $DownloadedBytes = $Global:DPCEventArgs.BytesReceived if ($DownloadedBytes -ne $null) { Write-Progress -Activity "Downloading file: $($Name)" -Id 1 -Status "Downloaded bytes: $($DownloadedBytes)" -PercentComplete $PercentComplete } } until ($Global:DownloadComplete) } End { # Dispose of the WebClient object $WebClient.Dispose() # Unregister events used for tracking download progress Unregister-Event -SourceIdentifier WebClient.DownloadProgressChanged Unregister-Event -SourceIdentifier WebClient.DownloadFileComplete } } |