Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Powershell Download Script

Posted on September 11, 2015 By David Kittell
# If you haven't already done so you will need to run this to be able to execute PowerShell scripts
# You will need to run PowerShell as administrator to run this script
set-executionpolicy remotesigned

Pass variables to the script

<#
.SYNOPSIS
    Downloads one file from a defined URL to a defined directory path
.DESCRIPTION
    This script is designed to take a defined URL and download it to a defined directory on your computer.
    The script will assume the file name part of the URL is the file name for the computer.
.PARAMETER Path
.PARAMETER LiteralPath
.Example
    DownloadFile.ps1 http://www.kittell.net/downloadfile.txt c:usersdkittelldesktop
.Inputs
    [String]$WebURL
    [String]$FileDirectory
.Link
    
Powershell Download Script
#> # URL Parameter $WebURL = $args[0] # Directory Parameter $FileDirectory = $args[1] # If directory doesn't exist create the directory if((Test-Path $FileDirectory) -eq 0) { mkdir $FileDirectory; cd $FileDirectory; } # We assume the file you download is named what you want it to be on your computer $FileName = [System.IO.Path]::GetFileName($WebURL) # Concatenate the two values to prepare the download $FullFilePath = "$($FileDirectory)$($FileName)" # Give a basic message to the user to let them know what we are doing Write-Host "Downloading '$WebURL' to '$FullFilePath'" $uri = New-Object "System.Uri" "$WebURL" $request = [System.Net.HttpWebRequest]::Create($uri) $request.set_Timeout(15000) #15 second timeout $response = $request.GetResponse() $totalLength = [System.Math]::Floor($response.get_ContentLength()/1024) $responseStream = $response.GetResponseStream() $targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $FullFilePath, Create $buffer = new-object byte[] 10KB $count = $responseStream.Read($buffer,0,$buffer.length) $downloadedBytes = $count while ($count -gt 0) { [System.Console]::Write("`r`nDownloaded {0}K of {1}K", [System.Math]::Floor($downloadedBytes/1024), $totalLength) $targetStream.Write($buffer, 0, $count) $count = $responseStream.Read($buffer,0,$buffer.length) $downloadedBytes = $downloadedBytes + $count } $targetStream.Flush() $targetStream.Close() $targetStream.Dispose() $responseStream.Dispose() # Give a basic message to the user to let them know we are done Write-Host "`r`nDownload complete"
powershell -file DownloadFile.ps1 http://www.kittell.net/downloadfile.txt c:usersdkittelldesktop

Manually type values in the script execution

# Request the URL
$sWebURL = Read-Host -Prompt 'Input the URL to download from'

# Request the Directory to download to
$sFileDirectory = Read-Host -Prompt 'Input the directory path to download to (remember to include end )'

# We assume the file you download is named what you want it to be on your computer
$sFileName = [System.IO.Path]::GetFileName($sWebURL)

# Concatenate the two values to prepare the download
$sFullFilePath = "$($sFileDirectory)$($sFileName)"

# Give a basic message to the user to let them know what we are doing
Write-Host "Downloading '$sWebURL' to '$sFullFilePath'"

# Download the file
$webclient = New-Object System.Net.WebClient
$webclient.DownloadFile($sWebURL,$sFullFilePath)

# Give a basic message to the user to let them know we are done
Write-Host "Download complete"
powershell -file DownloadFile.ps1

References:
http://windowsitpro.com/powershell/prompting-user-input-powershell
https://teusje.wordpress.com/2011/02/19/download-file-with-powershell/
http://stackoverflow.com/questions/5592531/how-to-pass-an-argument-to-a-powershell-script

All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code PowerShell

Post navigation

Previous post
Next post

Related Posts

Linux/Unix/Mac OSX – Find & Remove Empty Directories

Posted on May 4, 2016January 29, 2024

Occasionally you get empty directories and need to clear them out, these scripts below will help with that, just be careful what directory you choose. The scripts below are set to ~/ (current user home directory) but can easily be changed to any directory on your system. # List empty…

Read More

Delete Logs Older Than 6 Months

Posted on February 25, 2013October 26, 2015

DELETE FROM LogTable WHERE log_datetime < dateAdd(m, -6, getDate()) Originally Posted on February 25, 2013Last Updated on October 26, 2015 All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test…

Read More

Last Read / Last Write

Posted on April 29, 2013October 26, 2015

Sometimes you need to know the last time a table or database was read from or written to, this code will help determine that for you. Originally Found At: http://blog.sqlauthority.com/2009/05/09/sql-server-find-last-date-time-updated-for-any-table/ USE <Database_name> SET ANSI_WARNINGS OFF; SET NOCOUNT ON; GO WITH agg AS ( SELECT last_user_seek, last_user_scan, last_user_lookup, last_user_update FROM sys.dm_db_index_usage_stats…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes