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

Ektron – Find DMS Content File

Posted on November 11, 2013October 26, 2015

Sometimes you’re asked where a DMS file exists on the server and have to track it down, this query should help. Caveat: It helps to know where your root/main asset directory is on your server. SELECT content_id AS "Content: Content ID" ,content_title AS "Content: Content Title" ,(storage + ‘.’ +…

Read More

Bash – Running In Docker?

Posted on October 26, 2020August 17, 2024

docker=$(cat /proc/self/cgroup | awk -F/ ‘$2 == "docker"’) if [ ! -z "$docker" ]; then echo "Running in Docker" else echo "Not running in Docker" fi Originally Posted on October 26, 2020Last Updated on August 17, 2024 All information on this site is shared with the intention to help. Before…

Read More

Chocolatey

Posted on October 20, 2015August 30, 2016

What is Chocolatey Chocolatey NuGet is a Machine Package Manager, somewhat like apt-get (UNIX), but built with Windows in mind. How to Install Open PowerShell ISE as admin (typical 64-bit path %windir%\sysWOW64\WindowsPowerShell\v1.0\PowerShell_ISE.exe) Set Execution Policy Permissions Run the installation script from https://chocolatey.org/install.ps1 get-packageprovider -name chocolatey Stop-Process -processName: powershell* -force #…

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