# 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.