Skip to content

Instantly share code, notes, and snippets.

@DBremen
Last active August 16, 2022 03:26
Show Gist options
  • Save DBremen/40602ad8342bd20b2ddf to your computer and use it in GitHub Desktop.
Save DBremen/40602ad8342bd20b2ddf to your computer and use it in GitHub Desktop.
Download files using the DownloadFile method of the WebClient class within the System.Net Namespace
function Get-FileWCSynchronous{
param(
[Parameter(Mandatory=$true)]
$url,
$destinationFolder="$env:USERPROFILE\Downloads",
[switch]$includeStats
)
$wc = New-Object Net.WebClient
$wc.UseDefaultCredentials = $true
$destination = Join-Path $destinationFolder ($url | Split-Path -Leaf)
$start = Get-Date
$wc.DownloadFile($url, $destination)
$elapsed = ((Get-Date) - $start).ToString('hh\:mm\:ss')
$totalSize = (Get-Item $destination).Length | Get-FileSize
if ($includeStats.IsPresent){
[PSCustomObject]@{Name=$MyInvocation.MyCommand;TotalSize=$totalSize;Time=$elapsed}
}
Get-Item $destination | Unblock-File
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment